Javascript arguments and javascript function Overloading

Source: Internet
Author: User

Javascript arguments and javascript function Overloading
1. All functions have their own arguments object, which includes the parameters to be called. It is not an array. If typeof arguments is used, 'object' is returned '. Although we can call arguments by calling the data method. For example, length and index methods. However, the push and pop objects in several groups are not applicable. 2. There is no relationship between the number of parameters in function definition and the number of parameters in function call. F. arguments [0] and f. arguments [1] gets the first and second parameters passed in when the call is obtained. arguments cannot be created and is a function's own parameter. It can only be used when the function starts execution. Although the use of arguments is similar to an array, it is not an array. The length of an arguments object is determined by the number of arguments rather than the number of arguments. Parameters are the variables used to re-store the memory space in the function, but they do not overlap with the memory space of the arguments object. 1 function argumentsTest (a, B) {2 // alert (typeof arguments); 3 alert (arguments. length); 4 alert (arguments [1]); 5} 6 argumentsTest (,); output result:; 1 function argumentsTest (a, B) {2 alert (arguments. callee); 3 alert (arguments. callee. length); 4} 5 argumentsTest (1, 2, 3); the output result is argumentsTest, 2; 3. according to the Declaration and calling features of functions in JavaScript, we can see that functions in JavaScript cannot be overloaded. Based on the reloads in other languages: "different function return values or different number of parameters", we can conclude that: first, Javascript function declaration does not return value type; second, the number of parameters in JavaScript is strictly to facilitate variable operations in the function. In fact, the real parameters are already stored in the arguments object. In addition, the JavaScript function itself has a deep understanding of why functions in JavaScript cannot be overloaded: In JavaScript, functions are actually objects, and function names are references to functions, or the function name itself is a variable. How to Implement javascript overloading? Use arguments to determine the number of parameters using different methods to achieve function overloading: 1 <script language = "JavaScript"> 2 function f (length) 3 {4 var len = arguments. length; 5 if (1 = len) 6 {7 var width = arguments [1]; 8 alert ("high:" + length + ", width:" + width ); 9} 10 else11 {12 alert ("high:" + length); 13} 14} 15 </srcipt> 4. the arguments object has a very useful property: callee. Arguments. callee returns the current function reference of the arguments object. We recommend that you replace the function name with arguments. callee when using a recursive function call. 1 function count (a) {2 if (a = 1) {3 return 1; 4} 5 return a + arguments. callee (-- a); 6} 7 8 var mm = count (10); 9 alert (mm );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.