js handwriting function call

= Function.prototype.myCall function (context, ... ARR) {
     IF (context === null || context === undefined) {
        // specify this value is null and undefined automatically the global object (browser for window) 
        context = window 
    } the else { 
        context = Object (context) // value of the original values (numbers, strings, Boolean) of this point will be the original value of the object instance 
    } 
    const specialPrototype = the Symbol ( 'special properties the Symbol ') // for temporarily storing function 
    context [specialPrototype] = the this ; // the this function is directed to the implicit binding context 
    the let context Result = [specialPrototype] (ARR ...);// performed by implicit binding function and pass parameters 
    Delete context [specialPrototype]; // delete the context object attribute 
    return Result; // returns the result of the function execution 
};
let test = {
    name: "test"
}, fun = {
    fn: function () {
          console.log(this.name)
    }
}
fun.fn.myCall(test)
context [specialPrototype] = this; personal understanding of this sentence 
function which context is when you use this myCall a pointing object
when you use the above method to an object which corresponds to this key is added a specialPrototype, value as a function of of this key-value pairs (points of this issue, when you use myCall, and this is the calling function myCall, that the above fun.fn, that is ultimately in the context which adds a specialPrototype: fun.fn)
context [specialPrototype] (... arr); when you execute this line of code is actually called fun.fn, but this time there's a point to this call specialPrototype the context becomes
so in this context is complete instead fun.fn in this
 

Guess you like

Origin www.cnblogs.com/web-chuan/p/11592261.html