Basic JavaScript Concepts

Source: Internet
Author: User

1. Understanding Parameters

JavaScript is part of ECMAScript, and its function is derived from ECMAScript. The parameters of the ECMAScript function are different from the Java language, where a call to a method in the Java language (a function in JavaScript) must satisfy both: the same method name, the same number of arguments, the same parameter type, and the same parameter order. And in ECMAScript, it doesn't mind passing in many parameters. That is, calling a function does not necessarily have to pass two parameters, which can be one, three, or even not passed.

In the underlying code, the ECMAScript parameter is internally represented by an array. The function receives this array forever, without caring about how many arguments are in the array. In a function, you can use a arguments object to access each element of an incoming parameter, whose Length property determines how many arguments are passed in.

Example 1:

1 function Hello () {2   alert (arguments[0] + "," +arguments[1]);   3 }

So the important feature of the ECMAScript function is that named parameters are only available to programmers for easy viewing, but are not required.

In the development process, this feature can be used to receive arbitrary parameters, and the corresponding functions are implemented separately.

Example 2:

1 function Doadd () {2    if (Arguments.length = = 1) {3        alert (arguments[0]+10); 4     } Else if (Arguments.length = = 2) {5        alert (arguments[0]+arguments[1]); 6    }  7 }

The operating result is:

1 doadd (ten);    // The result is 2 Doadd (10,30);    // The result is a

The arguments object can be used with the named parameter phase one, and its value is always synchronized with the value of the corresponding named parameter (note that this does not mean that both values will access the same memory space, their memory space is independent, but the value is in step, the effect is unidirectional, Modifying the value of a named parameter does not change the value of the arguments. )

Example 3:

1 function Doadd (num1,num2) {2     if (Arguments.length = = 1) {3           alert (num1+10); 4     } Else if (Arguments.length = = 2) {5          alert (arguments[0]+num2)   ; 6     }7 }    

2. No overloads

The ECMAScript function cannot implement overloading because it is represented by an array of 0 or more values, so the function is unsigned and cannot be overloaded if the function is not signed. If two functions with the same name are defined at the same time, the function defined later is the one that takes effect.

Example 1:

1 function addnum (num) {2       return num+100;   3 }45function  addnum (num) {6       return num+200;   7 }89var result = Addnum (100);

In the above code example, the correct output answer is 300, not 200. By checking the type and number of incoming function arguments, and then reacting differently, you can simulate the effect of a method overload in Java.

Basic JavaScript Concepts

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.