One, arguments use method
Access to each element through the square brackets syntax
var fun = function (one) {
console.log (arguments[0]);
Console.log (Arguments[1]);
Console.log (arguments[2]);
Fun (1, 2, 3)
//1
/2
//3
With length
properties, see how many parameters you want
function Fun () {return
arguments.length;
}
Fun (1, 2, 3)//3
Fun (1)//1
Fun ()//0
Parameter assignment ("not allowed in strict mode")
var fun = function (A, b) {
arguments[1] = 2;
return a + B;
}
Fun (1, 1)
//3
Ii. the relationship between arguments and array
Although arguments can use the bracket syntax and have length
attributes, the arguments object is just like an array, not Array
an instance. Therefore, you cannot use standard array methods for arguments variables, such as push, pop, or slice. Although for
it is possible to use circular traversal, it is best to convert it to a true array for better use of the array method.
How do I use an array method?
By apply
means of the method, arguments is passed as a parameter so that the arguments can use the array method.
For the Apply method
myfunction.apply (obj, arguments).
Use to merge Array.prototype.concat.apply with another array
([1,2,3], arguments)
The ultimate solution, directly into the real array
The following code creates a new array that contains the elements in all arguments objects.
var args = Array.prototype.slice.call (arguments);
or
var args = [];
for (var i = 0; i < arguments.length i++) {
args.push (arguments[i]);
or
var args = [].slice.call (arguments, 0);
Third, powerful but not recommended callee properties
The arguments callee
property can call the function itself, and it can be invoked when the function is executing, and recursive calls to the method are implemented
The following code will implement a method of finding the factorial
var factorial = function (x) {return
X<=1?1:x*arguments.callee (x-1);
}
In ECMAScript5 strict mode, the read-write operation of this attribute produces a type error and can significantly affect the performance of the modern JavaScript engine, so it is strongly recommended that you do not use arguments.callee
and its properties.
Iv. Summary
The above is the entire content of this article, I hope the content of this article for everyone's study and work can bring certain help, if you have questions you can message exchange.