手写call,apply方法实现
call
Function.prototype.myCall = function(){ var object = arguments[0]; var arr = []; for(var i = 1; i < arguments.length; i++){ arr.push(arguments[i]); } object.__proto__._fn = this; var result = object._fn(...arr); delete object.__proto__._fn; return result; }
apply
Function.prototype.myApply = function(object,arr){ object.__proto__._fn = this; var result = object._fn(...arr); delete object.__proto__._fn; return result; }