The status of "arguments [n] cannot be set if n is greater than the number of formal or actual parameters" error?

You can set individual elements of a argumentsfunction property (that Mozilla calls an array-like property), however Mozilla messages cannot add elements to this property in SpiderMonkey 1.5, although this is fixed in 1.6 (link to SpiderMonkey here ... https: //developer.mozilla.org/en/JavaScript/Reference/Functions_and_function_scope/arguments ).

This is a useful property, chaining constructors from subclasses, creating a list of arguments to jump to a function (e.g. myclassmethod.apply(this, arguments)), etc.

However, I found that V8 will not extend the length in the same way as Mozilla reports on SpiderMonkey 1.5. (Not sure what the status is with other JavaScript, Opera, Rhino, etc. mechanisms).

Is this an ECMA feature? Was Mozilla wrong when considering this error, or does V8 have an error to fix?

[Update] I found that with V8 you can assign a property arguments.length, and therefore argumentscan be effectively extended (or set to whatever length you need). However, JSLint complains that this is a bad assignment.

[Refresh] Some test codes, if someone wants to try this in Opera, FF, etc., create an instance of a subclass calling the constructor with one argument, adding an element to argumentsin the constructor of the subclass and calling the superclass constructor, the superclass should report two arguments :

function MyClass() {
    if (arguments.length) {
        console.log("arguments.length === " + arguments.length);
        console.log("arguments[0] === " + arguments[0]);
        console.log("arguments[1] === " + arguments[1]);
    }
}

function MySubClass() {
    console.log(arguments.length);
    //arguments.length = 2;  // uncomment to test extending `length' property works
    arguments[1] = 2;
    MyClass.apply(this, arguments);
}

MySubClass.prototype = new MyClass();

new MySubClass(1);

[Update] JSLint really complains when you perform any assignment argumentsin appearance (for example, arguments[0] = "foo"). Therefore, perhaps JSLint can also do here.

+3
source share
3 answers

Not sure if this is what you are looking for, but you can turn the arguments object into a standard array:

var args = [].slice.call(arguments, 0);

[EDIT] so you can:

myclassmethod.apply(this, [].slice.call(arguments, 0).push(newValue));
+2
source

ECMA-262 10.1.8 , arguments arguments , " ". , , , . , .

0

, , .

FF, Comodo Dragon, Opera IE ( , Windoze), arguments.length ( Mozilla , ) arguemnts . , , (.. arguments[arguments.length] = "new arg").

, javascript , , "array-like" arguments , ;) ECMA , , , ( , ).

MSDN, Mozilla, , ( , V8 Opera JS-), - , , . , , - , - , .

Thanks to everyone who took the time to look at this question.

[Update] BishopZ indicated above that it .applywill accept an array of arguments anyway (this has slipped since I used it many years ago and which solves my problem). However, "array-like" argumentscan still be used and may even be preferable in some situations (it is easier, for example, to display a value on or simply pass arguments to the superclass). Thanks again for your attention.

0
source

All Articles