JS Learning Summary----function

Source: Internet
Author: User

First, basic knowledge of functions

1, Function: Functions, a method with a function. The method itself is meaningless, only the implementation of this method to achieve the corresponding function has its own value.

2. The function consists of two parts: creating a function, executing the function

function method Name () {

function Body: The JS code that is implemented by a function that is stored

}

Method name ();

Creating a function can be performed more than once, and in general, there is no relationship between executing the function and the last execution

function sum () {            varnull;             = 1 + 1;            Console.log (total)        }
The sum//is followed by no parentheses, which represents the current function itself, sum ();//sum, which represents the purpose of executing the current function function: To turn those strings stored before the function into JS code,
Then the JS code is executed from top to bottom.

 The process of creating a function:

1), first open a new memory space, the browser assigns it a 16-binary address, we assume that the address is: xxxfff000

2), the JS code in the function body as a " string " stored in the memory space.

3), and then assign this address to the current function name sum->xxxfff000

Procedure for function execution

1), when the function executes, it first provides a private scope, which is used for code execution in the function body

2), the code string in the function body into code from top to bottom execution

Note: The function at the time of execution, the first will form a new private scope, for the function body code from top to bottom execution, the formation of the private scope, the code in our function body wrapped up, to protect the private variables inside, not affected by the outside world, we put the function of the formation of this protection mechanism called " Closed Package "

Second, formal parameters

Extension: We want to upgrade the sum method, just give me two numbers to sum the parameters 

function sum (num1,num2) {//NUM1 and num2 is the parameter "variable" We set is the two entry            varnull for the current function ;             = Num1 + num2;            Console.log (total)        }        sum (100,200); // when executing,  simply pass the value of the corresponding parameter to the function.        // NUM1 =  num2 = Undefined The parameter is not passed by default is undefined

Third, return

The return in the function is the exit provided to us by the current function, because the closure mechanism causes the outside of the function body because it does not provide an exit.

Unable to get the value inside, if you want to use the value inside, the value needs to return the total value to the outside--"return value mechanism"

  Which value we want to use outside, we return the value to->return total; instead of returning the variable to the outside, it returns the value stored in the variable to the outside

var total = SUM (100,200)//The function sum is executed first, then the return value after the sum is executed (see the return result of sum execution, just see if there is a return in sum, what is the value returned after Return,return, So the return value of the function is what, if there is no return in sum then the default return value is undefined) assigned to the outside of the variable total

Return in function body the code below does not perform the function of code execution in the body

Iv. arguments

"Sum of any number": Give me a few numbers (I do not know, there is no way to define the number of parameters), I can find and

  Arguments: The function is inherently self-bringing (whether written or not, regardless of whether the value is passed or not, arguments always exists) to receive a collection of parameter values passed in by the function

typeof arguments//Object It is a value of an object data type, it is indexed by numbers, the index is incremented from zero, index 0 stores the first parameter, index 1 stores the second parameter value, and index n stores the value of the n+1 parameter ... There is a property called length that represents the total number of arguments passed; it's not an array, we call it an array of classes.

Callee:function sum () ... It also has a special property of its own callee, which represents the current function itself 

function sum () {            varnull;              for (var i = 0;i<arguments.length;i++) {
var cur = number (Arguments[i])
For non-valid numbers we are not accumulating to prevent the final result being Nan
if (IsNaN (cur)) {
       Continue
}
Total + =cur ; } return Total ; }

Five, anonymous function

Real-Name function: The function that has been named is the real function

anonymous function: A function without a name is an anonymous function

There are two ways to use anonymous functions in JS:

1), self-executing function: Define function and function execution are done together

The function of;(function (num) {front Plus; is to prevent the above code from ending without adding;

} (100))

2), function expression: Assign the part of a function definition as a value to a variable or an element's behavior

var f = function () {}

  

JS Learning Summary----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.