The JavaScript authoritative guide function

Source: Internet
Author: User

First, function definition

A function declaration statement declares a variable and assigns a function object to it. The definition function expression does not declare a variable.

If a function definition expression contains a name, the local scope of the function contains a name that is bound to the function object. The name of the function becomes a local variable inside the function.

A function declaration statement is "advanced" to the top of an external script or external function scope and can be called by code that appears before it is defined. But a function defined by an expression cannot.

Second, function call 1. function call

If the function expression is a property access expression, that is, the function is a property of an object or an element of an array, it is a method call expression

2. Method invocation

O.M ();

The object o becomes the calling context, and the function body can use this reference to read and write

When a method return value is an object, it can also call its method again. When the method does not need a return value, it is best to return this.

This is a keyword and is not allowed to assign a value to this. This does not have a scoped limit, and the nested function does not inherit this from the function that called it. If the nested function is called as a method, its this value points to the object that called it. If a nested function is called as a function, its this value is undefined (in strict mode) for many global objects (not strict mode). If you want to access the this value of an external function, you need to keep this in a variable that is in the same scope as the inner function. This is usually saved with the variable self.

varo ={m:function() {        varSelf = This; Console.log ( This= = = O);//truef (); functionF () {//nested function f ()Console.log ( This= = = O);//falseConsole.log (self = = = O);//true        }    }}; 

3. Constructor calls

If the constructor does not have formal parameters, you can omit the argument list and parentheses.

The constructor call creates a new object that inherits from the prototype property of the constructor, and considers the object here as its calling context.

4. Indirect calls

Call () apply ()

Functions as namespaces

Defines a function as a temporary namespace in which the defined variables do not contaminate the global namespace.

// because I think the function of the first declaration below can be appended with a parenthesis () to execute itself, such as Foo (), // because Foo is just a reference to the function () {/* code */}-This expression var function /* */ } ()

When the parser resolves the global function or function keyword, the default is to think of the function declaration, not the function expression,
If you do not show the compiler, it is declared as a function with a missing name by default, and a syntax error message is thrown because a function declaration requires a name.
/*//

All we need to do is enclose the code in braces, because the parentheses () in JavaScript cannot contain statements.

So at this point, when the parser parses the function keyword, it parses the corresponding code into a function expression, not a function declaration.

// The following 2 brackets () will be executed immediately (function/**/ / recommend this (function/* */ //  But this is also available  

Defines an anonymous function that is called in a single expression.

(function() {    // Code } ());

The left parenthesis before function is required, and if not written, the function is expected to declare the statement. Use parentheses to resolve to function definition expressions

Closed Package

Function objects can be interconnected by scope chains, and variables inside the function body can be stored within the scope of the function, which becomes a closure.

// returns function f () {return A;} var a = ' global '; function Check () {    var a = ' local ';     function f () {return  A;}     return F;} Check ()

Closures can capture local variables (and parameters) and keep them.

// Check () returns only one function object nested inside the function, rather than returning the result directly.
Returns the local scopevar a = ' global '; function Check () { var a = ' local '; function f () {return A;} return F;} Check () ()

Each time a JavaScript function is called, a new object is created to hold the local variable and add the object to the scope chain. When the function returns, the object of the bound variable is removed from the scope chain. If there are no nested functions, and no other references point to the bound object, it will be treated as garbage. If a nested function is defined, each nested function corresponds to a scope chain, and the scope chain points to a variable binding object.

If the function defines a nested function and returns it as a return value or stores it in a property, then an external reference is directed to the nested function, and the variable bound object it points to is not garbage collected.

//defines a function that is called immediately, and the return value of the function is assigned to a//The count variable cannot be accessed by other code after the external function returnsvarUniqueinteger = (function() {                        varCount = 0; return function() {returncount++}; } ()); Uniqueinteger//Function ObjectUniqueinteger ()//0Uniqueinteger () ()//1

//returns a function that always returns vfunctionConstfuncs (v) {return function() {returnv;}; }varFuncs = []; for(vari = 0; I < 10; i++) Funcs[i] =Constfuncs (i); alert (funcs[5] ())//5//these closures are defined in the same function call and can share the variable ifunctionConstfuncs () {varFuncs = [];  for(vari = 0; I < 10; i++) Funcs[i]=function() {returni;}; returnFuncs;}varFuncs =Constfuncs (); Alert (funcs[5] ())//Ten

function properties, methods, and constructors

Length Property

Prototype property

Call () apply () method

Bind () method binds a function to an object

ToString () method

function () constructor

You can pass in any number of string arguments, and the text of the last argument is the body of the function.

No incoming function name required

The compilation of the function body code is always performed at the top-level function, which can be thought of as the Eval () performed at the global scope

var scope = "global"; function constructfunction () {    var scope = "local";     return New Function ("Return scope");} // returns global because functions returned through the function () constructor use a local scope constructfunction () ()

The JavaScript authoritative guide function

Related Article

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.