Solved

JavaScript example, whats wrong here?

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.NET MVC 5- How can I post textarea value using @Html.ActionLink? 8 62
Date on a table 16 37
CSS - Centering an image 2 28
Secure log in 'box' 4 14
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
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…
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.

730 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