Solved

JavaScript example, whats wrong here?

Posted on 2014-04-08
3
201 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

837 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