my problem here that i don’t understand how does the method called “objectchanger” works
here is the source
function test()
{
this.value=5;
}
test.prototype.Add=function()
{
this.value++;
};
var obj = new test();
obj.Add();
alert(obj.value);
function objectchanger(fnc, obj)
{
fnc.call(obj);
//obj.fnc(); >> without this line of code it works fine but why?????
//why i don't need to write this code --
}
objectchanger(obj.Add, obj);
alert(obj.value); // the value is now 7
callis a method on theFunctionobject. It calls a function with the passed-in object as thethisvalue in the function. See the MDN docs on call.So when
objectchangercallsfnc.call(obj), it is callingtest.prototype.Add.call(obj), which is the same as callingobj.Add().