手动实现一个bind方法


var p={
age:20
};
function Person(name,sex){
console.log(this);
console.log(this.age);
console.log(name,sex)
}
Function.prototype.bindy=function(context){
var _self=this,
args=Array.prototype.slice.call(arguments,1),
tempFn=function(){};
var fn = function(){
var newArgs=Array.prototype.slice.call(arguments);
_self.apply(this instanceof _self ? this : context,args.concat(newArgs));
}
tempFn.prototype=this.prototype;
fn.prototype=new tempFn();
return fn;
}


var p2=Person.bindy(p,'张三','男');
new p2('男');

 

 

 

posted @ 2020-09-03 17:15  菜鸟程序员的总结  阅读(390)  评论(0编辑  收藏  举报