JavaScript Core--function

Source: Internet
Author: User

What is: An object that holds a reusable piece of code

when : as long as a piece of code may be used repeatedly, to encapsulate as a function , call the function repeatedly

How to :

created : 3 kinds :

1. Direct Volume : function name ( parameter list ){

function Body ;

return value ;

}

missing : will be declared ahead of time, disrupting the execution sequence of the program

Solution :

2. Assignment : var function name =function (argument list) {

function Body ;

return value ;

}

Superior : will not be declared in advance

Revelation : A function is actually an object of a reference type

The function name is actually just a normal variable

function names refer to function objects by address

function is actually the meaning of creating a new function Object

3. using NEW:

var fun=new function (" parameter 1", " parameter 2",..., "Functional body ")

overloaded (overload):

What is: the same function name, multiple functions of different parameter lists, when called, can automatically select matching function execution according to the parameters passed in .

Why : reduce The number of APIs and reduce the burden on callers

When: As long as a task, depending on the parameters passed in, the different process will be executed.

How to :

problem : The JS syntax does not support overloading by default because multiple functions with the same name are not allowed to exist at the same time!

Solution : Arguments

Anonymous functions :

What is : a function that does not specify a function name when defining a function

When: When a function is used only once

why : Save Memory

How to : 2 kinds :

1. Callback function : After defining a function, it does not execute itself, to other functions , and other functions to control the invocation and execution of the process.

2. anonymous function self-tuning : After function definition, call yourself !

Why : global variables, run out, can't be released automatically! Pollution Overall !

workaround : prohibit the use of global variables in the future !

when : only the code of all functions must be placed in the anonymous function self-tuning

How to : (function () {

function body

})();

Scope (scope):

What is : the available range of variables

why : ensure that internal variables do not affect / interfere with external programs

includes : 2 kinds :

1. Global scope : Window

Save Global Variables : features : available everywhere , reusable !

2. Function scope:

Save Local variables : features : only available within the function, not reusable !

The Life cycle of a function:

1. before The program begins execution :

1. Create execution environment Stack (ECS): used to record How many functions are being called

2. Record the first invoked browser main program (main) in ECS

3. Create a global scope Object window, save All the global variables that are required for the main function to execute

2. When defining a function :

1. defining variables with function names

2. Create a function object to hold the definition of the function , the function name variable refers to the functions object by the address

3. Scope property of The function object , which refers to the scope from which the function is returned

3. when the function is called:

1. Add a record of this function call to ECS

2. Create a function scope object for this function call ( active object AO)

3. Save All local variables in the active object AO

4. give the parent Property of the AO, the scope object that points to the scope Reference of the function

The order in which variables are used during invocation:

First , using local variables in the AO

Local no , just go to the global find

4. after the function call:

1. The record of this function call in ECS, the stack

2. cause The function scope object AO release

3. Causes local variable release in AO

Scope Chain (Scope chain):

What is a chain structure that is formed by hierarchical scope object referencing

Save all the variables

Controls the order in which variables are used : first Local, then global

Closed Package

What is : A mechanism that reuses variables and protects them from contamination

why : both global and Local variables have an irreplaceable advantage and Disadvantage

Global : excellent : can be reused, short : available everywhere, easy to be contaminated

local : excellent : only within the function is available, will not be contaminated

missing : not reusable !

when : future as long as you want a variable, can be reused, and will not be contaminated

How to : 3 steps :

1. wrapping protected variables and inner-layer function objects with an outer function

2. The outer function returns the Inner function object to the outer

3. The user calls the outer function to get the inner layer function returned

missing : The closure takes up more memory space than the normal function

fix : if the closure is no longer used, it should be released as soon as possible!

assign a variable that references a closure structure to NULL

Garbage collection

What is : the engine automatically frees the memory space of objects that are no longer used

Why : The size of the memory space is Limited! The system frees objects that are no longer in use and frees more space for subsequent new objects .

Garbage Collector: a small program that specifically counts and reclaims the space of objects that are no longer being used in memory

How to :

1. The garbage collector starts with the main program startup and runs in the background with the main program running.

2. The garbage collector records that each object is referenced by several variables

3. Whenever an object is no longer referenced by any variable, the garbage collector automatically releases the object

habit : as long as a large object is no longer used, the variable should be actively assigned to NULL

JavaScript Core--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.