when reading the book You Don't Know JS: this & Object Prototypes
I found this example about this
binding:
function foo(something) {
this.a = something;
}
var obj1 = {
foo: foo
};
var obj2 = {};
obj1.foo( 2 );
console.log( obj1.a ); // 2
obj1.foo.call( obj2, 3 );
console.log( obj2.a ); // 3
var bar = new obj1.foo( 4 );
console.log( obj1.a ); // 2
console.log( bar.a ); // 4
I don't understand why after the execution of new obj1.foo(4) console.log(obj1.a)
prints 2
and not 4
. And if I comment the line obj1.foo(2)
the result of the log above is undefined
.