Kuan
Kuan

Reputation: 11389

Running order of JS constructor

One question about JS constructor function:

var hm = {};
new function(name){
    hm[name] = this;
}("hello")

Could anyone give me a little explanation about how this constructor running (such as which part runs first)

Upvotes: 0

Views: 583

Answers (1)

StackSlave
StackSlave

Reputation: 10627

First var hm becomes an Object, then a new Anonymous function is called with ("hello") passed into name argument. The important thing to understand here is that when you see () with or without any arguments after a function name or an Anonymous function, it calls the function. In this case the new keyword and the fact that there is a this property inside the function makes it a Constructor. hm inside the Constructor creates a property based on the name argument and assigns the new instance itself to hm[name], as this refers to each new instance. End result is that hm.hello or hm['hello'] now refer to the new instance. Of course, all code runs from top to bottom and according to standard order of operations, like String resolution before assignment. Also, note that, this won't work:

func('wow');
var func = function(x){
  console.log(x);
}

This will work:

func('wow');
function func(x){
  console.log(x);
}

If you lack understanding of Constructors altogether you should know that a Constructor is used so you can have multiple instances of similar Objects. For instance:

function Person(last, first, middle){
  this.lastName = last; this.firstName = first; this.middleName = middle;
  this.said = this.ate = '';
  this.saySomething = function(anything){
    this.said = anything;
    return this;
  }
  this.eatSomething = function(food){
    this.ate = food;
    return this;
  }
  this.didWhat = function(){
    var m = this.middleName ? ' '+this.middleName  : '';
    var n = this.firstName+m+' '+this.lastName;
    if(this.said){
      n += ' said, "'+this.said+'"';
      n += this.ate ? ', and' : '.';
    }
    if(this.ate){
      n += ' ate '+this.ate+'.';
    }
    return n;
  }
}
var Bob = new Person('Small', 'Bob', 'Richard');
Bob.saySomething('This programming stuff is pretty cool.').eatSomething('Some Vegan Food');
console.log(Bob.didWhat());
var Sally = new Person('Jones', 'Sally');
Sally.saySomething("It just takes time, but you'll get it.");
console.log(Sally.didWhat());

Remember that the keyword this refers to the instance itself. In the example above I have created Bob and Sally Objects by calling new instances of Person. By returning this within a Constructor method you can chain methods, since the result of the executing method with be the instance itself.

Note that

Bob.saySomething('This programming stuff is pretty cool.').eatSomething('Some Vegan Food');

is the same as

Bob.saySomething('This programming stuff is pretty cool.');
Bob.eatSomething('Some Vegan Food');

since, as far as .eatSomething() is concerned, Bob and this are synonymous.

If you just want to access a property, it's like:

console.log(Bob.said);
console.log(Bob.lastName);
Bob.said = "Now you're getting it.";
console.log(Bob.didWhat());

Upvotes: 2

Related Questions