admin管理员组

文章数量:1401401

Suppose I have the following code (pletely 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.

Suppose I have the following code (pletely 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.

Share Improve this question edited May 14, 2013 at 13:34 David Fregoli asked May 14, 2013 at 12:51 David FregoliDavid Fregoli 3,4071 gold badge21 silver badges40 bronze badges 4
  • 1 Why wouldn't it be "safe"? – Naftali Commented May 14, 2013 at 12:52
  • @Neal well arguments is a pretty strange beast, so some trepidation about its use is not unreasonable. In this case I can't imagine how it'd be a problem. – Pointy Commented May 14, 2013 at 12:53
  • @Neal, maybe because arguments doesn't have many of the methods a normal Array has, and .apply may want to use them? – Dogbert Commented May 14, 2013 at 12:54
  • 2 MDN writes: "Calls a function with a given this value and arguments provided as an array (or an array like object)". So I think you're safe :) – robertklep Commented May 14, 2013 at 12:55
Add a ment  | 

3 Answers 3

Reset to default 7

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/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/ecma-262/5.1/#sec-15.3.4.3, as was kindly pointed out by @Pointy.

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.

MDN can only speak for Mozilla implementations. The actual spec to which all implementations should ply 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.

本文标签: javascriptIs it safe to pass 39arguments39 to 39apply()39Stack Overflow