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 = {};
function myFunction(){
return this == myObject;
}
assert( !myFunction(),"Context is not set yet" );
var aFunction = myFunction.bind(myObject)
assert( aFunction(),"Context is set properly" );
对Jeffery下面的代码的微小修改帮助我了解内部匿名函数中使用的参数.我刚才改了3行
var introduce = function(greeting) { alert(greeting + ",my name is " + this.name + ",home no is " + arguments[1]); }
hiBob(" 456"); // alerts "Hi,my name is Bob"
yoJoe(" 876");
感谢大家
解决方法
arguments对象是一个类似数组的对象,它只有length属性.
通过Array.prototype调用切片功能是将其转换为数组的常用技术,因此您可以直接在此示例上使用像shift和concat这样的数组函数.