Start with this setting:
function fn() { console.log(this); } var thisvalue = {fn: fn};
Now you undoubtedly understand that thisvalue.fn() is a method call and sets the registered this value for thisvalue object.
Then you seem to know that fn.call(thisvalue) makes the exact same call. Alternatively, we could write (thisvalue.fn).call(thisvalue) (parentheses for structure only, which can be omitted) as thisvalue.fn === fn :
thisvalue.fn(âĻ); // is equivalent to (thisvalue.fn).call(thisvalue, âĻ); // or: (fn).call(thisvalue, âĻ);
OK, but fn.call(âĻ) is just a method call - the function call method is called on the fn function.
It may be available as such because all function objects inherit this .call property from Function.prototype - this is not a proprietary property like .fn for thisvalue object. However, fn.call === Function.prototype.call same as thisvalue.fn === fn .
Now we can rewrite this .call method .call as an explicit call with .call() :
fn.call(thisvalue); // is equivalent to (fn.call).call(fn, thisvalue); // or: (Function.protoype.call).call(fn, thisvalue);
I hope you notice the pattern and can now explain why the following work:
Function.prototype.call.call.call(Function.prototype.call, fn, thisvalue); var call = Function.prototype.call; call.call(call, fn, thisvalue);
Interrupting this is left as an exercise for the reader :-)
Bergi
source share