A multi-faceted understanding of ECMAScript function parameters

Source: Internet
Author: User

Write in front

In any programming language, the function is a particularly interesting part, but it is also a difficult point. In ECMAScript, the function as an object is no exception, and it makes people love and hate. In this chapter we talk about a number of aspects of the function parameters this part, as the function of the sea as deep as the other parts we have the opportunity to talk.

Some of the knowledge points of the function are relatively simple, so before we get to the point, let's briefly introduce two points: the function and the overloading of return functions, because the overloading of functions requires arguments knowledge, so we finally understand the overloads of the functions.

The chapters are structured as follows:

    • Return of the function
    • function parameters
    • Overloading of functions

Note: If there is no additional explanation, the function in this article refers to the function in ECMAScript.

Return of the function

returnAs the name implies 返回 , the function return stops and returns immediately after execution, that is, the return subsequent statement is never executed. As follows:

function functionReturn(){return ‘over;console.log(‘something after return‘);}

something after returnis never going to be executed.

Note: There may be ambiguity here. What is said here return is immediately followed by return , like this is not included:

function fn() {(function(){for(var i in a){return(i);}})();console.log(‘hello world‘);}var a = [1, 2, 3];fn();

Here return , the back of the print can still output normally, this should not be difficult to understand. Comments in the Netizen 韩子迟 has proposed this ambiguity, hereby corrected, but also thanks 韩子迟 to the proposed.

function parameters

About function Parameters I summed up 8 points, and we parted.

1. The function has no limit on the number of arguments passed in, and there is no limit to the data type of the parameters passed in. That is, if there are 2 parameters, but the arguments passed in can be 1, or 3, and the parameter type does not matter.

2. Each function is called, its active object will get two special variables, one of which is the arguments object, in the object to arguments save the arguments passed to the function, in the body of the function can be arguments accessed through the object of the parameter array, so that each argument can be obtained, but note that , arguments an object is just an object that resembles an array, and it is not an Array instance.

Look at an example:

function funcAdd(num1,num2){console.log(num1+num2);console.log(arguments);}funcAdd(100,200);

When the function is called, the funcAdd argument is saved in the arguments object, so the print result is 300 , [100,200] . We can access the object like an array arguments , as follows:

function funcAdd(num1,num2){console.log(num1+num2);console.log(arguments[0],arguments[1],arguments[2]);}funcAdd(100,200);

Because only two parameters are passed, the printed result is,, 100 , 200 undefined .

3. From the above example, it is also possible to conclude that a named parameter that does not pass a value is assigned a value undefined .

4. It is not necessary to name parameters (which should be well understood by the formal parameter).

As follows:

function funcAdd(){console.log(‘传递给函数的两个参数是:‘+arguments[0]+‘和‘+arguments[1]);}funcAdd(1,‘para2‘);

In this function, the name parameter is not explicitly given, but the arguments first two values in the object are used, and the printed result is: 传递给函数的两个参数是:1和para2 , so the named parameter is not required. Explicitly write named parameters one is the custom, the second is to provide a visual and programmatic convenience.

5. With regard to named parameters, other programming languages such as C require a function signature, that is, function name, number of arguments, parameter type, return value and other related declaration information, and then call the function must be consistent with the function signature, but the function in ECMAScript does not have a function signature. This is described in the overloads of the following functions. At the same time, it is because no ECMAScript does not have a function signature, so the function cannot implement overloading.

6. The object of the function arguments has an attribute that can be thought of as the length of the length array (the arguments object is just an object similar to an array, it is not Array an instance), thus obtaining the number of arguments passed to the function

As follows:

function funcAdd(){console.log(arguments.length);}funcAdd();funcAdd(1);funcAdd(1,2);

This code only prints out the number of arguments passed to the function when the function was called, and the result is: 0``1``2 (previously written 123, corrected).

7. The value of the named parameter is always arguments synchronized with the value in (the arguments value in), which is one-way, that is, the value arguments in will synchronize the value of the named parameter.

As follows:

function funcAdd(num1,num2){arguments[0]=20;console.log(num1+num2);}funcAdd(10,10);

The printing result is: 30 . Because for named arguments and arguments , the function is called arguments . Just generally, a value that has a named parameter is saved to the arguments procedure in.

Note: Although the value of the named parameter is always arguments synchronized with the value in (the arguments value in), their memory space is independent and does not share the same memory space, only the result two values will be synchronized.

8. All parameter passes in ECMAScript are passed by value, and access variables are accessed by value and accessed by reference in two ways.

First, say the basic type of replication. The basic type of replication is completely independent, for example value1=value2 , the values here are value2 copied to value1 , the two values are completely independent, arbitrarily modify one of them, and the other value will not change.

However, the copy of the reference type is different, unlike the copy of the underlying type, where the reference type replicates the pointer, which points to the same object in the heap memory, so if you modify one of the copied variables, the other variable changes as well.

var o1=new Object(),o2=o1;o1.color=‘red‘;o2.color=‘blue‘;console.log(o1.color);console.log(o2.color);

Because o1 o2 the same object is referenced, one of the references is modified, and the other changes accordingly. So the printing results are: blue``blue .

Next, the value is passed.

The delivery of a primitive type value and the copying of a basic type variable, a reference to the value of a type is the same as the copy of the reference type value, just copy the reference, for the function, the value outside the function is passed to the inside of the function, that is, the value of the outside of the function is copied to the inside of the function, and

With some theory of function parameters, let's talk about function overloading.

No overloads

Overloading of functions means that the function name is the same, but the parameter list (the number of parameters, the data type of the parameter) is not the same. In other programming languages such as C + + (previously written by C,c without overloading, which is hereby corrected), overloads are achievable, but in ECMAScript, functions are not overloaded. Look at the code first:

function funcAdd(num1,num2){console.log(num1+num2);}function funcAdd(num){console.log(num);}funcAdd(100,200);funcAdd(100);

In ECMAScript, the function that is located at the back of the same name overrides the previous function, so the function that is finally called is funcAdd(num) , in execution, because the function parameter of the console.log(funcAdd(100,100)); call is only one, passing in arguments two arguments 100 and, that is 200 , arguments[0] equal 100 to , which is arguments[1] equal 200 to, but only passed when the function is called arguments[0] , in order to verify that we look at the following piece of code separately:

function funcAdd(num){console.log(num);console.log(arguments);}funcAdd(100,200);

The results are printed 100 and the [100,200] above conclusions are validated.

While there are no overloads, we can simulate overloading mechanisms to detect data lengths and data types, thus executing different code to emulate overloaded implementations. Let's simply write a piece of code:

function funOverride(){if(arguments.length==1){console.log(‘Hi, ‘+arguments[0]+‘.‘);}else{console.log(‘Please input one para.‘);}}funOverride();funOverride(‘Jim‘);

Here, we judge the number of arguments separately to determine the different outputs, which in some way simulates overloading. Of course, this is just a simple piece of code, and it's a lot worse to fully implement overloading.

A multi-faceted understanding of ECMAScript function parameters

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.