Summary of JavaScript functions (i)

Source: Internet
Author: User

  1. a function is a program fragment that can be called and executed any time. JavaScript functions are parameterized: the definition of a function includes a list of identifiers called formal parameters that work like local variables in the body of a function. A function call gives the parameter the value of the argument. The function uses the values of their arguments to calculate the return value. The function uses the values of their arguments to evaluate the return value and becomes the value of the function call expression. In addition to the arguments, each invocation also has another value-the context of this invocation-this is the value of the This keyword.

  2. If a function is mounted on an object, as a property of an object, it is called a method of the object. When the function is invoked through this object, the object is the context of the call. that is, the value of this function . The function used to initialize a newly created object is called a constructor.

  3. function declaration Statement "Advanced" to the top of an external script or external function scope, so a function declared in this way can be called by the code before it is defined. However, a function declared with an expression is another matter, in order to invoke a function, it must be able to refer to it, but it must be assigned to a variable before it is used in an expression-defined function. The declaration of a variable is advanced, but assigning a value to a variable is not advanced, so a function defined in an expression cannot be called until it is defined.

  4. according to ECMASCRIPT3 and non-strict ECMAScript5 for function calls, the invocation context ( the value of this) is a global object. In strict mode, however, the invocation context is undefined.

  5. The difference between a function and a method: a function is a normally defined function, whereas a method is a function property of an object.

  6. There is an important difference between a function call and a method call: The calling context. A property access expression consists of two parts, an object and a property name. In this invocation, the object becomes the calling context, and the function body can refer to the object using the keyword this .

  7. This is a keyword, andJavascript syntax does not allow for this assignment.

  8. method chain: When the return value of a method is an object, the object can call its method again. In this method call sequence (often called "chain" or "cascade") the result of each invocation is part of another expression. When the method does not need to return a value, it is best to return the this Api This api

  9. Unlike variables, the keyword This does not have scope restrictions, and nested functions do not inherit this from the function that calls it . If a nested function is called as a method, its this value points to the object that called it. If the nested function is called as a function, its this value points to the object that called it. If a nested function is called as a function, its this value is not a global object (in non-strict mode) or undefined(strict mode). Many people mistakenly assume that this will point to the context of the calling outer function when the nested function is called. If you want to access this value of this external function, you need to keep the value of this in a variable, which is in the same scope as the memory function. Usually use self to save this,eg: In-depth understanding of the toggle of this context

1) First look at the operation results of the program:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/4C/EA/wKioL1RHUlnBnWFaAAEO8Qi5OaI111.jpg "title=" 1 (1). PNG "style=" Float:none; "alt=" wkiol1rhulnbnwfaaaeo8qi5oai111.jpg "/>

2) Then follow the process of JS parsing execution: variable declaration in advance

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/4C/EA/wKioL1RHUlnAN3GJAAHSubEk_Yg394.jpg "title=" 1 (1). JPG "style=" float:none; "alt=" wkiol1rhulnan3gjaahsubek_yg394.jpg "/>

3) Variable assignment

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/4C/E9/wKiom1RHUgzSTZGQAAHkssPc8nw394.jpg "title=" 1 (2). JPG "style=" float:none; "alt=" wkiom1rhugzstzgqaahksspc8nw394.jpg "/>

4) function execution

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/4C/EA/wKioL1RHUlnzVnC_AAFDt2tYTmM852.jpg "title=" 1 (3). JPG "style=" float:none; "alt=" wkiol1rhulnzvnc_aafdt2tytmm852.jpg "/>

5) First equality judgement

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/4C/EA/wKioL1RHUlrwQLiAAAEIe8aWeis650.jpg "title=" 1 (4). JPG "style=" float:none; "alt=" wkiol1rhulrwqliaaaeie8aweis650.jpg "/>

6) Execute inside the function

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/4C/E9/wKiom1RHUgzRf9g6AAD4kuu6Cb8820.jpg "title=" 1 (2). PNG "style=" Float:none; "alt=" wkiom1rhugzrf9g6aad4kuu6cb8820.jpg "/>

So why does this here change? We can understand that the function is a program fragment, once executed, the useless things are collected by GC(garbage collection), when executing the internal nested function, first execute the function definition in the child function, the variable declaration statement, once found that the parent variable is still called in the child function, Then there is a variable that points back to the address of the variable in the parent function, and then cleans up the parent variable's reference to the original resource (including the variable and function), leaving only the global variable referenced in the child function being reported to be preserved (this is the closure), so this is the equivalent of the child function bare drain outside, so it is easy This points to Window .


This article is from "Tiger Brother's Blog" blog, please be sure to keep this source http://7613577.blog.51cto.com/7603577/1566701

Summary of JavaScript functions (i)

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.