自己实现一个bind函数
2020-07-20 本文已影响0人
前端小旋风
Function.prototype.myBind = function (){
if(typeof this !== 'function') throw new TypeError('@');
var fn_self = this,
fn_args = Array.prototype.slice.call(arguments,1);
var fn_empty = function (){}
var fn = function (){
return fn_self.apply(this instanceof fn ? this : arguments[0],fn_args.concat(Array.prototype.slice.call(arguments)))
}
if(this.prototype){
fn_empty.prototype = this.prototype;
}
fn.prototype = new fn_empty();
return fn;
}