JavaScript忍者秘籍这本书中 有一段Prototype库,函数bind代码的示例:
Function.prototype.bind = function(){
var fn = this, args = Array.prototype.slice.call(arguments),
object = args.shift();
return function(){
return fn.apply(object,args.concat(Array.prototype.slice.call(arguments)));
};
};
var myObject = {a:"1"};
function myFunction(){
return this == myObject;
};
var aFunction = myFunction.bind(myObject);
aFunction();
我用断点查看 函数bind里面的fn指向myFunction这个函数 这个我不太理解 我的理解是只要用Function.prototype 这个原型扩展的方法 此方法里面开始声明的变量var fn=this;中fn的指向就指向使用这个方法的函数 就像本例中的myFunction.bind(myObject);调用bin方法,fn指向myFunction这个函数 不知道这样理解对不对