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");
感谢大家