解释John Resig的一些忍者代码 [英] Explaining some of John Resig's ninja code
本文介绍了解释John Resig的一些忍者代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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行
Tiny modification to Jeffery's code below helped me understand the arguments used in the inner anonymous function. I just changed the 3 lines below
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");
谢谢大家
推荐答案
参数对象是一个类似数组的对象,它只有length属性。
The arguments object is an array-like object, it has only the length property.
通过Array.prototype调用slice函数是将它转换为数组的常用技术,因此您将能够使用数组函数,如 shift 和 concat ,直接。
Calling the slice function through the Array.prototype is a common technique to convert it to an array, so you will be able to use array functions like shift and concat on this example, directly.
这篇关于解释John Resig的一些忍者代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文