Solved

JavaScript example, whats wrong here?

Posted on 2014-04-08
3
205 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

Question has a verified solution.

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

Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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:
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

695 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