JavaScript Scope Learning Notes

Source: Internet
Author: User

First, study notes:

1. Scope and scope chain in JavaScript

+ Each function creates its own execution environment (scope) when called, and everything in JavaScript is an object, and the function object, like other objects, has properties that are accessed through code and internal properties that are accessible only to the JavaScript engine. One of the internal properties is [[scope]], which is created when the function is defined and contains

The function is created in the scope of the collection of objects. This set is called the scope chain of the function, and the scope chain determines what data can be accessed by the function.

The front end of a scope chain is always a variable object in the context of the currently executing code (the front end of the scope chain is an active object, and the active object is created when the function is called, which is composed of local variables, named parameters, parameter sets, this in the function), Querying identifiers in a function is a reverse lookup from the front end of the scope.

2 . Pre-compilation of JavaScript

+ JS execution process is a translation of the process, JS also has the process of compiling, JS in the execution of each JS code, will first deal with the VAR keyword and function definitions (function definitions and functional expressions). That means they're going to be declared ahead of time. However, the declaration of a function expression cannot be advanced, and the function expression is calculated at the time of execution.

3, reduce the use of global variables

Because global variables are always at the end of the scope chain, it is the slowest to find them. When a cross-scope object is referenced more than once, it is first stored in a local variable and then used. (For example, the document that is often used in DOM operations, you can first have a local variable inside the document)

4 . The functions in JavaScript run in the scope they are defined in, not in the scope in which they are executed.

Second, code verification

var name = "Youyi"; the function GetName () {///functions Create an active object before being executed,//JS pre-compiles the var keyword, which means that the name variable is declared in advance alert (name);// Undefinedvar name = "Hello";} GetName ();//The function is run in the scope where the function is defined, not in the scope of the runtime Factory () {   var username = ' Laruence ';     var intro = function () {          alert (' I am ' + username);//I Am laruence     }     return intro;} function app (para) {     v AR username = para;     var func = Factory ();     Func ();} App ("hahaha");

JavaScript Scope Learning Notes

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.