Home >Web Front-end >JS Tutorial >arguments及arguments.callee
First there is a JavaScript function
Code
<span style="font-family: 'courier new', courier;">function test(a, b, c, d) { return a + b; }</span>
The number of actual parameters when calling a function in JavaScript may not match the number of formal parameters of the called function. The requirements are not as strict as in Java, because in ECMAScript The parameters are represented internally by an array. When the function is called, it always receives this array, regardless of which parameters are contained in the array, or even if there are no elements.
Js code
<span style="font-family: 'courier new', courier;">function test(a, b, c, d) { return a + b; }
console. log(test(10, 20));54bdf357c58b8a65c66d7c19c8e4d114
Such code will not report an error in JavaScript. At the same time, in JavaScript we can get the number of actual parameters and formal parameters through the following code
Js code
<span style="font-family: 'courier new', courier;">function test(a, b, c, d) { console.log(test.length);//这里获得的是形参的个数 console.log(arguments.length);//这里获得的是实参的个数,这段代码必须放在函数内部 }
console.log(test(10, 20));54bdf357c58b8a65c66d7c19c8e4d114
There is also a similar object called arguments.calee, which is usually used for recursive calls
Js Code
<span style="font-family: 'courier new', courier;">function test2(num) { if(num <= 1) return 1; else return num*arguments.callee(num-1); } console.log(test2(5));</span>
If you change arguments.callee(num-1) to test2(num-1), an error will be reported in the following call
Js code
<span style="font-family: 'courier new', courier;">var F = test2; test2 = null; console.log(F(5));</span>