www.webdeveloper.com
Results 1 to 4 of 4

Thread: Named or unnamed constructor, What's the difference?

  1. #1
    Join Date
    Oct 2013
    Posts
    7

    Named or unnamed constructor, What's the difference?

    Below are 2 constructor objects, the only difference is the naming. What are the implications of using one or the other?

    var PersonT1 = function(name, age, gender){
    this.name = name;
    this.age = age;
    this.gender = gender;
    this.getGender = function(){
    return this.gender;
    };
    };

    var PersonT2 = function PersonT2(name, age, gender){
    this.name = name;
    this.age = age;
    this.gender = gender;
    this.getGender = function(){
    return this.gender;
    };
    };

    var user1 = new PersonT1("Maria",30,"female");
    var user2 = new PersonT2("Ben",40,"male");

    console.log(user1);
    console.log(user2);

    console.log(user1.constructor.name);
    console.log(user2.constructor.name);

    console.log(user1.getGender());
    console.log(user2.getGender());

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,654

  3. #3
    Join Date
    Oct 2013
    Posts
    7
    What's the point with a FORUM like this when people ask you to read. I do that to! Only that none of the books/tutorials gave an answer to my question. I guess when you have a lot of experience it's easy to figure out anything!!
    Here is the answer i was looking for from dynamicdrive.com
    /*
    Virtually none in this case except that some browsers might not like the second named constructor the way it's written there. In fact they're both "named" (via being assigned to a variable name). The first one is PersonT1, the second is named PersonT2. The PersonT2 syntax is redundant though. It could be simply:
    function PersonT2(name, age, gender){
    this.name = name;
    this.age = age;
    this.gender = gender;
    this.getGender = function(){
    return this.gender;
    };
    }
    And that's the usual syntax for declaring a constructor function.

    A truly unnamed constructor would look like so:
    var userX = new (function(name, age, gender){
    this.name = name;
    this.age = age;
    this.gender = gender;
    this.getGender = function(){
    return this.gender;
    };
    })("Sally", 35, "female");
    console.log(userX.getGender()); // gives: female

    jscheuer1 @dynamicdrive.com

  4. #4
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,654
    Quote Originally Posted by hvip View Post
    What's the point with a FORUM like this when people ask you to read...
    dear friend, let me inform you that english is not my native language and if you would like to try walking in my shoes just ask me your question in russian and then read my answer in russian. everybody comes here with his own problems to solve.

    i didn't ask you to read anything, it was just a hint of where you can find the information you were looking for. the answer from another resource is not full, because your question was about the difference between named and anonymous functions (Named or unnamed constructor, What's the difference?). In JavaScript, the constructor is a normal function which is used as an operand of the new operator.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles