JavaScript Advanced Programming (3rd Edition) Note--chapter3: Basic concept (function part)

Source: Internet
Author: User

3.7 Functions

3.7.1 Understanding Parameters

    • The ECMAScript function does not mind passing in multiple parameters, nor does it care what data type the arguments are passed in. Because the arguments in the ECMAScript are internally represented by an array. This parameter array can be accessed by the arguments object in the body of the function to obtain each parameter passed to the function.
    • Named parameters are provided for convenience only, but are not required.
    • The arguments object can be used with named parameters.
    • The value of the arguments object is always synchronized with the value of the corresponding named parameter. However, this does not mean that reading these two values will access the same memory space. Their memory space is independent, but their values are synchronized.
    • The length of the arguments object is determined by the number of arguments passed in, not by the number of named arguments when the function is defined. Named parameters that do not pass a value are automatically assigned the undefined value.
    • All parameters in the ECMAScript are passed as values, and arguments cannot be passed by reference.

Arguments the relationship of the object to the named parameter:

1 functionSay_hello (var1, var2, var3) {2      varLen =arguments.length;3 alert (len);4       for(Iincharguments) {5 alert (arguments[i]);6      }7 alert (var1);8 alert (VAR2);9 alert (VAR3);Ten } One  ASay_hello ();//0, undefined, undefined, undefined -Say_hello ("first");//1, first, first, undefined, undefined -Say_hello ("First", "second", "third");//3, first, second, third, first, second, third theSay_hello ("First", "second", "third", "forth");//4, first, second, third, Forth, first, second, third
1 function Say_hello (var1, var2, var3) {2       for inch arguments) {3          arguments[i] = "Change"; 4      }5      alert (var1 + var2 + var3); 6 }78//Changechangechange

3.7.2 not overloaded

    • Without a function signature, a real overload cannot be done.
    • If two functions with the same name are defined in ECMAScript, the name belongs only to the post-defined function.
    • You can simulate the overloading of a method by examining the type and number of parameters in the incoming function and reacting differently.

No overloads

1 function overload (var1) {2     alert ("This is the first function"); 3 }45function  overload (Var1, Var2) {6     alert ("This is the second function "); 7 }89//This is thesecond function

Simulate overloading

1 functionoverload () {2     if(Arguments.length = = 0) {3Alert ("First");4}Else if(Arguments.length = = 1) {5Alert ("Second");6}Else {7Alert ("Third");8     }9 }Ten  OneOverload ();// First AOverload (1);//Second -Overload (1,2,3,4);//Third

JavaScript Advanced Programming (3rd Edition) Note--chapter3: Basic concept (function part)

Related Article

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.