Results 1 to 2 of 2

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

  1. #1
    Join Date
    Oct 2013
    Location
    Sweden
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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?

    Code:
    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());
    Last edited by keyboard; 11-19-2013 at 04:31 AM. Reason: Format: Code Tags [code][/code]

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,980
    Thanks
    43
    Thanked 3,195 Times in 3,157 Posts
    Blog Entries
    12

    Default

    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:

    Code:
    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:

    Code:
    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
    Last edited by jscheuer1; 11-18-2013 at 06:02 PM. Reason: add unnamed constructor
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following 2 Users Say Thank You to jscheuer1 For This Useful Post:

    hvip (11-18-2013),XManBG (11-18-2013)

Similar Threads

  1. Resolved c# .NET - constructor
    By NDK in forum Other
    Replies: 1
    Last Post: 11-30-2009, 09:16 AM
  2. What is Instance, Empty Constructor ....
    By tking88 in forum Java
    Replies: 0
    Last Post: 10-23-2009, 01:40 PM
  3. Glossy menus and named anchors
    By kmhurley in forum Dynamic Drive scripts help
    Replies: 1
    Last Post: 09-24-2008, 11:50 PM
  4. Named Anchors Don't Seem To Work
    By tomyknoker in forum Flash
    Replies: 0
    Last Post: 05-20-2007, 07:16 AM
  5. Replies: 2
    Last Post: 04-12-2007, 06:33 PM

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
  •