JS Basic Learning 05

Source: Internet
Author: User

17. Function part Content Supplement 17.1 function four forms

1 no parameter no return value: Commonly used for code reuse, or function code snippets.

2 No parameter has a return value: The return value after the function is called is independent of the caller.

Function F1 () {
return 20;
}

3 parameter no return value: The function has parameters, but there is no specific return value.

function Sayhi (name) {
Console.log ("Hello" +name);
}

4 has the parameter return value (the most used type in JS)

function Getmax (arr) {
var max = arr[0];
for (Var i=1;i<arr.length;i++) {
max = max<arr[i]?arr[i]:max;
}
return Max;

}

Note: No return value refers to the absence of a return statement, but not to a true value, when the return value defaults to undefined.

17.2 Overloading of functions

The meaning of overloading: refers to the function that has the same function name, but the number of arguments of the function is different or the type of the parameter is different, then the corresponding function can be selected by the number of function parameters and the data type of the parameter.

However, in JS, if there is a function with the same name, the function will overwrite the previous function, the previous function can no longer be called.

function Getsum (A, b) {
return a+b;
}
function Getsum (a,b,c) {
Return a + b +c;
}
var sum = getsum (10,20);

Console.log (sum);

If the above code declares two functions with the same function name but different parameters of the function, the final code will output Nan, that is, the second function is selected for invocation execution. (Note: In this function, C is not assigned, all functions default C is undefined, and the conversion to number type data is Nan.) The result of Nan's participation in the operation is still Nan. )

17.3 declaring a function in the form of a function expression

A function that declares a function by assigning a value to a variable that has no function name. The function that corresponds to the variable name can be called by the variable name + () when the function is called.

var fun = function (A, b) {
return a-B;
};
Fun (50,70);

17.4 Anonymous functions

An anonymous function is a function that does not have a function name, and a function declared in the form of a function expression is an anonymous function.

17.5 How a function is declared differs from a function expression

1 functions have function names in the way they are declared;

2 functions in the method of the function expression is not a function name, is an anonymous function;

3 When a function is pre-parsed, the function declaration is preceded by the first of the scope, and the expression of the function is not promoted;

4 functions that are declared in a function can be called again, but functions with function expressions must be preceded by a function expression statement.

17.6 pre-parsing of functions

Before the program executes the code, all the code is scanned once, the declaration of all variables, function declaration, function parameters are promoted to the front of the current scope, the function is declared by the declaration of the function is the whole promotion, but the function is declared by the function expression can only be part of the declaration of the variable elevation, And the expression part of the function is still left in the original code position is not promoted, all if the function expression before the call function, the program will error indicates that the variable does not define a value.

17.7 self-executing functions

Self-executing functions refer to functions that can be executed on their own.

(function () {
Console.log ("This is a F2 function");
})();

(function () {
Console.log ("This is a F3 function");
Console.log ("See if I have done it");
}());

This is how two self-executing functions are expressed. The self-executing function does not have a function name, and the function is called immediately after it is defined.

This function is often used for one-time use, avoiding repetition and conflict of function names.

18. Scope of variables

There is only global scope and local scope in JS, there is no block-level scope.

18.1 Block-level scopes

This variable is a block-level variable, and the {} interval is a block-level scope if the variable is declared separately in the interval between {} and cannot be called outside the {} interval.

There is no block-level scope in JS. Such as:

{var m = 100;}
Console.log (m);

In JS, variables declared within the {} interval can be read outside the {} interval.

18.2 global scope

A variable can be read anywhere, and this is a global variable, and the global scope refers to the area between all the script tags in the page.

There are two types of global variables:

1 variables declared within the global scope.

2 In addition to function parameters inside the function, variables defined without VAR are also global variables.

18.3 local Scope

If a variable declared within a function cannot be read outside the scope of the function, it means that the variable is a local variable, and this function is a local scope.

JS Basic Learning 05

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.