function Set() { // This is the constructor
this.values = {};
this.n = 0;
this.add.apply(this, arguments); // All arguments are values to add
}
// Add each of the arguments to the set.
Set.prototype.add = function() {
/* Code to add properties to the object's values property */
return this;
};这是"Javascript:权威指南“中用来创建”Set“类的代码的开始。我试图使构造函数中apply()函数的必要性变得合理,但终究弄不明白。
this.add.apply(this, arguments);如果add()函数已经是'this‘调用的方法,那么核心apply()函数实现的目的或用途是什么。提前感谢任何试图向我解释这一点的人
http://jsfiddle.net/Marlin/Ydwgv/ -来自Javascript的完整示例:权威指南
发布于 2011-09-13 11:48:04
“如果add()函数已经是由'this‘调用的方法,那么核心应用()函数实现的目的或用途是什么。”
这样arguments就可以作为整个集合传递。如果Set接受不确定数量的参数,则此函数非常有用。
https://stackoverflow.com/questions/7396552
复制相似问题