Function.prototype.bindOne=function(context){
if(typeof this!=='function'){
throw new Error('bound is not callable')
}
//留住this
var self=this;
//arguments转数组,并从index=1截取
var args=Array.prototype.slice.call(arguments,1);
var fNop=function (){};
var fBound=function (){
var boundArgs=Array.prototype.slice.call(arguments);
return self.apply(this instanceof fNop?this:context,args.concat(boundArgs))
}
//原型链继承
fNop.prototype=this.prototype;
fBound.prototype=new fNop();
return fBound;
}
var foo={
value:1
}
function bar(){
console.log(this.value);
}
var a=bar.bindOne(foo)
a();