I have an object literal like this:
var test = {
one: function() {
},
two: function() {
this.one(); // call 1
test.one(); // call 2
}
};
What is the difference between the calls in the two function (using the object literal name versus using this)?
testis always bound within the closure of thetwofunction to the variabletestwhereasthisdepends on how the function is called. If the function is called using the regular object member access syntax,thisbecomes the object owning the function:You can change the value of
thisby usingFunction.prototype.call:But the value of
testremains the same inside thetwofunction, regardless of how the function is invoked.