itnifl
asked on
JavaScript example, whats wrong here?
<div id="one"></div>
<div id="two"></div>
<div id="three"></div>
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)
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
If I do this customer.name also works:
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?
var customer = new function(n) {
this.name = n;
$('#one').html('Inside: ' + n);
}('Olaf');
$('#three').html('Outside-2: ' + customer.name)
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?
Open in new window