SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: Functions / new

  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2010
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Functions / new

    Hi - can anyone explain to me what is the difference between the two codes below in relation to how the 'friend' variable is assigned, more specifically I am trying to understand what exactly 'new' does behind the scenes, apart from the obvious answer that it creates an object :

    Code:
    function Person(name, age, job){
    var o = new Object();
    o.name = name;
    o.age = age;
    o.job = job;
    o.sayName = function(){
    alert(this.name);
    };
    return o;
    }
    
    var friend = new Person(“Nicholas”, 29, “Software Engineer”);
    friend.sayName(); //”Nicholas”
    Code:
    function Person(name, age, job){
    //create the object to return
    var o = new Object();
    //optional: define private variables/functions here
    //attach methods
    o.sayName = function(){
    alert(name);
    };
    //return the object
    return o;
    }
    
    var friend = Person(“Nicholas”, 29, “Software Engineer”);
    friend.sayName(); //”Nicholas”

  2. #2
    SitePoint Addict dnordstrom's Avatar
    Join Date
    Jul 2006
    Location
    Amsterdam
    Posts
    337
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    The `new` keyword instantiates an object, running the code in the "function" as a constructor. Your examples are a bit odd. Since you're returning an object, you probably wouldn't need to call it with `new`. See Crockfords articles for details on this, I know it can be confusing:

    http://javascript.crockford.com/private.html
    http://javascript.crockford.com/prototypal.html
    http://www.crockford.com/javascript/
    Daniel Nordstrom. of. Nintera(ctive)
    -- Featured post: Part 2. Writing NI.JS JavaScript
    ----- Follow me on Twitter. Got project? Contact me.
    -------- SitePoint: Community Guidelines Be A Great Member

  3. #3
    SitePoint Addict
    Join Date
    Apr 2011
    Posts
    265
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    In the first function, alert(this.name); displays the value of the property "'name" of the object.
    In the second function, alert(name); displays the value of the parameter "name" passed to the function.
    So, you can get different results if the "name" property is set different, like: o.name = 'my'+ name;
    The 'new' creates a new instance of that object, so, you can use multiple instances of the same object, in separated variables.
    Free: Web Programming Courses HTML, CSS, Flash
    Web Programming: AJAX Course and PHP-MySQL Course video Lessons
    Good JavaScript and jQuery course for beginners

  4. #4
    SitePoint Enthusiast
    Join Date
    Jun 2010
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi - thanks for the documentation and the explanations, I think I understand now that the second example is a pattern used for specific circumstances for example where data security is an issue whereby there is no way to access any of it's data members without calling a method - otherwise one should create instances using the 'new' keyword and by doing so you can also make use of inheritance when creating custom objects.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •