Home > Web Front-end > JS Tutorial > arguments及arguments.callee

arguments及arguments.callee

巴扎黑
Release: 2016-11-25 09:38:55
Original
1092 people have browsed it

First there is a JavaScript function

Code

<span style="font-family: &#39;courier new&#39;, courier;">function test(a, b, c, d) {  
    return a + b;  
}</span>
Copy after login

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: &#39;courier new&#39;, courier;">function test(a, b, c, d) {  
    return a + b;  
}
Copy after login

console. log(test(10, 20));

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: &#39;courier new&#39;, courier;">function test(a, b, c, d) {  
    console.log(test.length);//这里获得的是形参的个数  
    console.log(arguments.length);//这里获得的是实参的个数,这段代码必须放在函数内部  
}
Copy after login

console.log(test(10, 20));

There is also a similar object called arguments.calee, which is usually used for recursive calls

Js Code

<span style="font-family: &#39;courier new&#39;, courier;">function test2(num) {  
    if(num <= 1) return 1;  
    else return num*arguments.callee(num-1);  
}  
  
console.log(test2(5));</span>
Copy after login

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: &#39;courier new&#39;, courier;">var F = test2;  
test2 = null;  
console.log(F(5));</span>
Copy after login


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template