call、apply

手写call方法

  Function.prototype.call = function(content){

    content = content ? Object(content) : window;

    content.fn = this;

    let args = [ ];

    for(var i = 1; i < arguments.length; i ++){

      args.push(arguments[i])

    }

    let res = content.fn(...args);

    delete content.fn;

    return res;

  }

手写apply方法

  Function.prototype.apply = function(context, args) {

    context = context ? Object(context) : window;

    context.fn = this;

    if (!args) {

      return context.fn();

    }

    let res = context.fn(...args);

    delete context.fn;

    return res;

  }

原文地址:https://www.cnblogs.com/cuishuangshuang/p/13433606.html