Solved

JavaScript example, whats wrong here?

Posted on 2014-04-08
3
195 Views
Last Modified: 2014-04-09
<div id="one"></div>
<div id="two"></div>
<div id="three"></div>

Open in new window


var customer = new function(name) {
    this.name = name;
    $('#one').html('Inside: ' + name);
    return {
        getName: function() {
            return name;
        },
        setName: function(newName) {
            name = newName;
        }
    };
}('Olaf');
customer.setName('Albert');
$('#two').html('Outside-1: ' + customer.getName());
$('#three').html('Outside-2: ' + customer.name)

Open in new window


Output:

Inside: Olaf
Outside-1: Albert
Outside-2: undefined

Why is the last output undefined? Didn't I set a property called name with a value?
0
Comment
Question by:itnifl
3 Comments
 
LVL 18

Expert Comment

by:Jerry Miller
ID: 39987259
I would use a different identifier from 'name', there may be some conflict using multiple instances of name since it is a keyword. I think that you were setting this.name to itself and that was producing your undefined error.

var customer = new function(n) {
    this.name = n;
    $('#one').html('Inside: ' + n);
    return {
        getName: function() {
            return n;
        },
        setName: function(newName) {
            n = newName;
        }
    };
}('Olaf');
customer.setName('Albert');
$('#two').html('Outside-1: ' + customer.getName());
$('#three').html('Outside-2: ' + customer.name)

Open in new window

0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39987296
Why is the last output undefined?

Open in new window


What do you return?
You're not returning the name

Check this page : http://jsfiddle.net/Q8q3h/

var Customer = function(name) {
    $('#one').html('Inside: ' + name);
    return {
        name:name,
        getName: function() {
            return this.name;
        },
        setName: function(newName) {
            this.name = newName;
        }
    };
}

var customer = new Customer("Olaf");
customer.setName('Albert');
$('#two').html('Outside-1: ' + customer.getName());
$('#three').html('Outside-2: ' + customer.name)

Open in new window

0
 
LVL 2

Author Comment

by:itnifl
ID: 39988026
If I do this customer.name also works:
var customer = new function(n) {
    this.name = n;
    $('#one').html('Inside: ' + n);
}('Olaf');

$('#three').html('Outside-2: ' + customer.name)

Open in new window


But if I choose to return what attributes are available like in the question, then this.name = n; will not set an attribute named name. Does it get overwritten by the return statement or what happens to it?
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now