Is it safe to pass 'arguments' to 'apply()'

Suppose I have the following code (completely useless, I know)

function add( a, b, c, d ) {
  alert(a+b+c+d);
}

function proxy() {
    add.apply(window, arguments);
}

proxy(1,2,3,4);

Basically, we know that apply expects an array as the second parameter, but we also know that arguments is not a proper array. The code works as expected, so is it safe to say that I can pass any array-like object as the second parameter in apply()?

The following will also work (in Chrome at least):

function proxy() {
  add.apply(window, {
    0: arguments[0],
    1: arguments[1],
    2: arguments[2],
    3: arguments[3],
    length: 4
  });
}

Update: It seems that my second code block fails in IE<9 while the first one (passing arguments) works. The error is Array or arguments object expected, so we shall conclude that it's always safe to pass arguments, while it's not safe to pass an array-like object in oldIE.

Answers


Assuming ECMAScript 5.1: Yes. As per ECMA-262, 10.6, the arguments object has the length and index properties that 15.3.4.3 (Function.prototype.apply) requires.


From definition of Function.prototype.apply in MDN:

fun.apply(thisArg[, argsArray])

You can also use arguments for the argsArray parameter. arguments is a local variable of a function. It can be used for all unspecified arguments of the called object. Thus, you do not have to know the arguments of the called object when you use the apply method. You can use arguments to pass all the arguments to the called object. The called object is then responsible for handling the arguments.

REF: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Function/apply

As the second argument apply accepts "an array like object, specifying the arguments with which function should be called". To my understanding, this array-like object should have the length property for internal iteration, and numerically defined properties (zero-indexed) to access the values.

And the same is confirmed my the spec: http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.4.3, as was kindly pointed out by @Pointy.


MDN can only speak for Mozilla implementations. The actual spec to which all implementations should comply says the following:

15.3.4.3   Function.prototype.apply (thisArg, argArray)

When the apply method is called on an object func with arguments thisArg and
argArray, the following steps are taken:

1.  If IsCallable(func) is false, then throw a TypeError exception.
2.  If argArray is null or undefined, then
      Return the result of calling the [[Call]] internal method of func,
      providing thisArg as the this value and an empty list of arguments.
3.  If Type(argArray) is not Object, then throw a TypeError exception.
4.  Let len be the result of calling the [[Get]] internal method of argArray
      with argument "length".
5.  If len is null or undefined, then throw a TypeError exception.
6.  Let n be ToUint32(len).
7.  If n is not equal to ToNumber(len), then throw a TypeError exception.
8.  Let argList be an empty List.
9.  Let index be 0.
10. Repeat while index < n
      a. Let indexName be ToString(index).
      b. Let nextArg be the result of calling the [[Get]] internal method of
         argArray with indexName as the argument.
      c. Append nextArg as the last element of argList.
      d. Set index to index + 1.
11. Return the result of calling the [[Call]] internal method of func,
      providing thisArg as the this value and argList as the list of arguments.

The length property of the apply method is 2.

NOTE The thisArg value is passed without modification as the this value. This
     is a change from Edition 3, where an undefined or null thisArg is replaced
     with the global object and ToObject is applied to all other values and that
     result is passed as the this value.

It seems that property length and numeric index values are the only prerequisites.


Need Your Help

Illegal character error in jquery

jquery select alert

I'm selecting the text value of the selected value of a drop down list. But i get this error. illegal character rname is just a string value. Can anyone help me solve this?

What perl data structure is this?

perl data-structures

my $struct = { 1 =&gt; "Image", 2 =&gt; "Audio", 3 =&gt; "Video" };

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.