In the below binding snippet:
function foo(something) {
this.a = something;
}
var obj1 = {};
var bar = foo.bind( obj1 );
bar( 2 );
console.log( obj1.a ); // 2
var baz = new bar( 5 );
console.log( obj1.a ); // 2
console.log( baz.a ); // 5
In this step var bar = foo.bind( obj1 ); we are binding obj1 which is empty to the foo function .
After executing bar(2) , obj1 value 2 .
Want to know what bar(2) had triggered?
My Assumption:
Since bar is assigned to foo and binded with obj1 , invoking bar(2) could have assigned the this.a = 2 and kept that value in obj1(i.e.obj1 = { a: 2 }).
Is my assumption right?