1. call 实现
    1. (function(){
    2. if(!Function.prototype.call) {
    3. Function.prototype.call = function(context=window, ...args) {
    4. if(typeof this !== 'function') {
    5. throw new TypeError("this not a function");
    6. }
    7. context.fn = this;
    8. let result = context.fn(...args);
    9. delete context.fn;
    10. return result;
    11. }
    12. }
    13. })()
    1. apply 实现
    1. (function(){
    2. if(!Function.prototype.apply) {
    3. Function.prototype.apply = function (context=window, ...args) {
    4. if(typeof this !== 'function') {
    5. throw new TypeError("this not a function");
    6. }
    7. context.fn = this;
    8. let result = args[0] ? context.fn(...args) : context.fn();
    9. delete context.fn;
    10. return result;
    11. }
    12. }
    13. })()