3.3 js function

Source: Internet
Author: User

1. function Syntax:

How the function is declared:
Function name (parameter 1, parameter 2 ...) {
function body;
}
Function call: Function name (parameter 1, parameter 2 ...);

The return value is not necessarily specified within the function.

If you need to specify a return value, you can use return to follow the value you want to return to specify the return value.

The code behind the return statement is not executed under the scope of the return statement (if the return statement is in a function body, then the return statement is in the same scope as the function body).

If the return statement does not follow the returned value, the return value is undefined. Applies to scenarios where return values are not required.

2. Parameters of the function

The parameters of the function in JS are different from the parameters of other language functions.

If you define a function, there are three parameters, in the call this function, you can not pass in parameters, you can also pass in 2 3 4 parameters,

The reason is that the parameters of the function in JS are represented in the form of an array (the array may contain 0 or more values) inside.

You can access the parameters of the function by arguments this object. Arguments is an array object.

As in the following example:

The parameters of the function in JS  argumentsfunction person (name,age,height) {    console.log ("name" + arguments[0] + "age" + arguments[1] + " Height "+ arguments[2]);    Console.log (arguments.length);} Person ("Lijinwen", "180cm"), print out the following values://  name Lijinwen age 10 Height 180cm//  3

According to this characteristic of the parameters of the JS function, we can actually imitate the function's overloaded function. That is, depending on the number of function parameters, different results are obtained. But this is not a function overload.

impersonation function overloading functions function fun () {    if (arguments.length = = 2) {        Console.log ("two parameters");    } else if (arguments.length = = 3) {        Console.log ("three parameters");}    } Fun (up);//two Parameters Fun (three-in-one);//Three parameters

3. The function is not overloaded. If you have two functions with the same name, then the function that follows will overwrite the previous one.

The function does not have overloaded functions sum (A, b) {    console.log ("two parameters");} function sum (a,b,c) {    console.log ("three parameters");} sum (UP);//three parameters sum (three-in-one);//Three parameters

We had thought that if we passed 2 parameters to the function, we would call the first function to print out "two parameters", and if 3 parameters, call the second function to print out "three parameters".
Look at the above code sum (2 parameters), the result prints out "three parameters", then you can conclude that it calls the second function.
As a result, the last function will overwrite the function with the same name in the case of multiple functions with the same name. This is also called overloading, so the function in JS is not overloaded.

3.3 js function

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.