New and Window binding

node v10.16.3
version: 1.0.0
endpointsharetweet
'New' binding
// This is a constructor function, and should be capitalized and called with the `new` keyword var Animal = function(color, name, type) { // this = {} this.color = color; this.name = name; this.type = type; }
var sayAge = function(){ console.log(this.age) } var me = { age:25 } /* When the 'this' keyword is used in the sayAge() function, there is nothing to the left of sayAge. As a result, 'this' defaults to the window object. sayAge, because it uses the `this` keyword, needs to be ran in the right context. */ sayAge(); // This returns `undefined` // We can explicitly set the context using call(), apply(), or bind() sayAge.call(me)
Loading…

no comments

    sign in to comment