4

Is there any difference between these 2 apart from the resolving constructor?

var Person = function(living, age, gender) {
    this.living = living;
    this.age = age;
    this.gender = gender;
    this.getGender = function() {
        return this.gender;
    };
};

var Person = function Person(living, age, gender) {
    this.living = living;
    this.age = age;
    this.gender = gender;
    this.getGender = function() {
        return this.gender;
    };
};

Both could be invoked using

var p = new Person("Yes",25,"Male");

The first one resolves to function() where the latter resolves to person(), but I would like to know if there is any advantage of using one over the other

2

1 Answer 1

6

They are identical for the purposes you speak of.

The only difference that inside the second function you have a clean reference to the function from within itself.

Formally

The language specification states:

FunctionExpression :

function Identifier(opt) ( FormalParameterListopt ) { FunctionBody }

The identifier (in this case Person) in the function expression is optional

The reasoning for that is explained a bit later in the language specification:

NOTE The Identifier in a FunctionExpression can be referenced from inside the FunctionExpression's FunctionBody to allow the function to call itself recursively. However, unlike in a FunctionDeclaration, the Identifier in a FunctionExpression cannot be referenced from and does not affect the scope enclosing the FunctionExpression.

In practice

You can use the second option in two situations:

When it makes your code more understandable:

   (function removeBodyDivs(){
        //does logic removing
        //divs from the body
   })();

Can be more understandable than:

   (function (){
        //does logic removing
        //divs from the body
   })();

When doing recursion, for example

  var f = function fib(n){
      return n<2?2:(fib(n-1)+fib(n-2));
  }
Sign up to request clarification or add additional context in comments.

13 Comments

But how does this relate to a constructor? For a normal function, that makes sense. But for a constructor, I can't see it having any effect. You never call the constructor from within
@Ian Good question, just because you should't do something doesn't mean people don't do it. You can, for example return an object from a constructor function which would make the new 99.9% meaningless. jsfiddle.net/NuT5F . I did start with "They are identical for the purposes you speak of."
@Ian: "You never call the constructor from within" --- imagine a tree-like structure. Some particular node of a particular type may create a nested node of the same type by default.
@Benjamin Gruenbaum: yep, for objects it makes sense.
Additional point: Having named constructor might be useful in debugging.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.