最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why is the apply() function needed in the constructor - Stack Overflow

programmeradmin4浏览0评论
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;
};

This is the beginning of the code used in "Javascript: The Definitive Guide" to create a 'Set' Class. I've tried to rationalize the necessity of the apply() function in the constructor but can not figure it out for the life of me.

this.add.apply(this, arguments);

if the add() function is already a method called by 'this' then what purpose or use is the core apply() function fulfilling. Thanks in advance to anyone who attempts to explain this to me

/ - Full Example from Javascript: The Definitive Guide

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;
};

This is the beginning of the code used in "Javascript: The Definitive Guide" to create a 'Set' Class. I've tried to rationalize the necessity of the apply() function in the constructor but can not figure it out for the life of me.

this.add.apply(this, arguments);

if the add() function is already a method called by 'this' then what purpose or use is the core apply() function fulfilling. Thanks in advance to anyone who attempts to explain this to me

http://jsfiddle/Marlin/Ydwgv/ - Full Example from Javascript: The Definitive Guide

Share edited Sep 22, 2011 at 13:46 Richard JP Le Guen 28.8k8 gold badges93 silver badges120 bronze badges asked Sep 13, 2011 at 3:43 MarlinMarlin 7494 silver badges8 bronze badges 4
  • It seems to serve little purpose. I have never seen it... – Blender Commented Sep 13, 2011 at 3:45
  • What are your thoughts on the fact that they are passing the arguments object? – ChaosPandion Commented Sep 13, 2011 at 3:48
  • you don't think it could have anything to do with ensuring that add() isn't added as a property of the instance instead of the prototype? i understand it isn't being declared...... but it sure is confusing me – Marlin Commented Sep 13, 2011 at 3:51
  • Calling .add() doesn't add it as a property. .apply() is used here only so you can pass on all of the arguments to .add() that were sent to Set() all at once. If you knew that Set() would always get 2 arguments, then you'd just name them as parameters Set( a, b ), and do this.add( a, b ). – user113716 Commented Sep 13, 2011 at 3:56
Add a ment  | 

5 Answers 5

Reset to default 7

"if the add() function is already a method called by 'this' then what purpose or use is the core apply() function fulfilling."

So that the arguments can be passed on as an entire collection. Useful if Set accepts an indefinite number of arguments.

It isn't need in the constructor -- in fact, it has nothing to do with a constructor in general (but obviously it matters for this specific one ;-). What apply does do is ensure that the target function is invoked "correctly".

That is, apply, when given an array-like object* (arguments counts), [n1, n2, ... n3] and a function f, it will call the target function as f(n1, n2, ... n3). apply also takes the "this context" which is passed the newly created object in the code (to make it seem as though add was called "as a normal method"). See the link above for more information/examples.

That means, that for the code above,

var s = new Set(1, 2, 3)

and

var s = new Set()
s.add(1, 2, 3)

are equivalent.

Happy coding.


*In FireFox prior to 4 it had to be an Array-proper. This is why sometimes code uses the ugly idiom:

f.apply(context, Array.prototype.slice.call(arguments, 0))

I believe it is being called not for the scope, but the fact that apply will take an array of arguments and pass them in order:

this.add.apply(this, [1,2,3,4]);
....
function add(one, two, three, four)

not a single parameter that is an array

The apply method of a function allows you to invoke the function by passing an array instead of a list of items.

To illustrate:

function add(a, b) {
    return a+b;
}

var values = [1,2];

alert(add.apply(this, values));

will alert 3.


So, essentially, what you are doing there is allowing your constructor to take any number of arguments, and you will then pass them all in to the add function in one shot.

So, if you were to call your constructor with:

var stack = new Set(1,2,3,4,5,6,7,8,9);

all of those values will be passed to the add function, as if you would have written:

this.add(1,2,3,4,5,6,7,8,9);

Using the .apply() syntax allows you to pass an array of arguments to the function being invoked. In this case it is passing the arguments object, which means it is calling add() with any and all of the parameters that were passed to the Set constructor function. So you can create new Set objects with an arbitrary number of values by saying, e.g.,

var mySet = new Set(13,42,18,19,44,11,5);

And all of those values will be passed through to add().

发布评论

评论列表(0)

  1. 暂无评论