风魔大王
2016-11-29 10:59
function foo(){ this.b=100; retrun this.a; } var func=foo.binddd({a:1}); Function.prototype.binddd=function(oThis){ var aArgs=Array.prototype.slice.call(arguments,1); fToBind=this; fNOP=function(){}; Bound=function(){ return fToBind.apply(this instanceof fNOP?this:oThis,aArgs.concat(Array.prototype.slice.call(arguments))); } fNOP.prototype=this.prototype; fBound.prototype=new fNOP(); return fBound; }; func(); new func();
function foo(){ this.b=100; return this.a; //retrun打错了 } var func=foo.bind({a:1}); //binddd打错了 Function.prototype.bind=function(oThis){ //binddd打错了 var aArgs=Array.prototype.slice.call(arguments,1); fToBind=this; fNOP=function(){}; Bound=function(){ return fToBind.apply(this instanceof fNOP?this:oThis,aArgs.concat(Array.prototype.slice.call(arguments))); } fNOP.prototype=this.prototype; fBound.prototype=new fNOP(); return fBound; }; console.log(func());//->1 console.log(new func())//->[object Object] {b: 100}(bind失效,忽略return,直接返回this{b=100}的对象)
JavaScript深入浅出
281097 学习 · 1020 问题
相似问题