A list,
Arguments before we get into arguments, let’s take a look at some of the features of javascript: Javascript doesn’t actually have the ability to override functions, but arguments objects can simulate overloading. Arguments every javascript function has a Arguments object instance that refers to the Arguments to the function, and can refer to the Arguments element as an array subscript “[]”. Arguments. length is the number of arguments to the function, and arguments.callee refers to the function itself.
Arguments. callee is a one-to-one mapping to the parameter
Second, the characteristics of
Arguments objects are inseparable from functions.
2. Since arguments the object cannot be explicitly created.
Arguments objects are only available at the start of a function.
Three, use method
While the Arguments object is not an array (an array of classes), individual arguments are accessed in the same way as array elements
Arguments [0],arguments[1]… The arguments [n]. You don’t need to specify parameter names in JS to access them
For example,
function test() { var s = ""; for (var i = 0; i < arguments.length; i++) { alert(arguments[i]); s += arguments[i] + ","; } return s; } test("name", "age"); The command output is name,ageCopy the code
Arguments.length -1 enumerates elements from 0 to arguments.length-1. Let’s look at the callee property, which returns the body of the Function object being executed. The Callee property is a member of the Arguments object and is only available when the related function is executing. The initial value of the callee property is the ** Function** object being executed. Implement anonymous recursive functions. The code is as follows:
var sum = function (n) { if (1 == n) { return 1; } else { return n + arguments.callee(n - 1); //6 5 4 3 2 1 } } alert(sum(6)); The command output is 21Copy the code
Arguments this object is mostly used when multiple calls to the same method and different numbers of arguments are passed. Determine the method to execute based on arguments’ index.
Knowledge expansion:
There are a few things to be aware of when using arguments for function passing. Examples are as follows:
var length = 10; function fn() { console.log(this.length); } var obj = { method: function(fn) { fn(); arguments[0](); }}; obj.method(fn, 1); Output: 10, 2Copy the code
Output: 10,2 there are two points to note here. This in the fn function refers to:
1. The first value is 10 and the first line of method “fn()” is executed, where this refers to the window. So the output value is the outermost length defined.
Arguments0 (arguments0 => fn())); arguments0 (arguments0 => fn()); arguments0 (arguments0 => fn())
That’s about all the uses and features of arguments. Maybe the Callee attribute is used less often. However, if you package or write some JS, except callee things will be used. I want my friends to pay more if something is wrong. We make progress together.
You can use argument to pass multiple arguments, such as the maximum value:
function max() {
var max = arguments[0];
console.log(arguments)
for (val of arguments) {
if (val >= max) {
max = val;
}
}
return max;
}
var maxValue = max('9', 1, 2, 4)
console.log(maxValue)
Copy the code