Arguments objects, overloading problems for JavaScript functions

Source: Internet
Author: User

I. Overview of Arguments objects:

1, the ECMAScript function does not mind passing in how many parameters, nor because the parameters are not uniform and error.

2. The function body can receive the passed in parameters through the arguments object, and save the function parameters

function box () {     return// Gets the value of each parameter, the last result is 1 | 2 | 6}alert (Box (//  Passing Parameters

Second, the length property of arguments:

The Length property under the arguments object can be used to get the number of arguments.

function box () {   return// get 6}alert (Box (1,2,3,4,5,6));

Using the length of this property, to intelligently determine how many parameters, and then the parameters of the rational application. For example, to implement an addition operation, all incoming numbers are added, and the number is indeterminate.

functionbox () {varsum = 0; if(Arguments.length = = 0){            returnSum//If there are no parameters, exit}Else{             for(vari = 0;i < arguments.length; i++) {//If you have one, accumulate it.sum = sum +Arguments[i]; }            returnSum//returns the cumulative result}}alert (Box (5,9,12));

Iii. properties of thecallee of arguments

  is a pointer to the function that owns the arguments object

1, a simple recursive:

function box (num) {        if (num <= 1) {            return 1;         Else {            return// A simple recursive         }    }    alert (Box (4) ); // 4*3*2*1 =  The result is

2. Recursive algorithm

Recursive algorithms are generally used for factorial functions, so the inside of a function must call itself

If the function name does not change is not a problem, but once the function name is changed, the internal call needs to be modified

In order to solve this problem, we can use Arguments.callee instead.

function box (num) {        if (num <= 1) {            return 1;         Else {            return num * Arguments.callee (num-1);       // use callee to execute itself and implement recursion         }    }    alert (Box (4)); // The results are also

Iv. overloading problems of functions

    The functions in ECMAScript do not function as overloaded functions as other high-level languages. (If there is a function with the same name, the last is the primary)

function box (num) {     return num +;}  function box (num) {                     // will execute this functions    return num + +;} Alert (box);                         // The returned result is 250, which is called the second function
function Box (num,a) {    return num +;}  function box (num) {                     // will execute this functions     return num + +;} Alert (Box (50,3));                         // The result also  indicates whether to execute the second function  

Arguments objects, overloading problems for JavaScript functions

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.