You don't know. JavaScript (roll up) reading notes two----lexical scopes

Source: Internet
Author: User

In the previous article, we defined scopes as "a set of rules for managing and maintaining variables," and then it's time to discuss the scope of JS in depth, first of all we need to know that there are two main types of work in scope, one is lexical scope, one is dynamic scope, JavaScript uses lexical scopes, and interested in dynamic scopes can be self-google.

1. Lexical phase

First of all, we have to understand the word "lexical phase", we have learned that JS has a compile phase, the first step in the compilation phase is the word segmentation/lexical analysis, we can be referred to as "lexical stage"

In simple terms, lexical scopes are defined in the lexical phase of the scope, the lexical scope is that you write code when the variables and block scope to write where to decide, after the Lexical analyzer processing code, in most cases will remain scope unchanged. We need to pay attention to the following points:

A. When the engine needs to query for a variable, it always starts from the current scope to find

B. Scope lookup stops when a first matching identifier is found

C. Masking benefit (the Internal identifier "obscures" the external identifier)

D. Global variables automatically become properties of global objects, which indirectly access those obscured global variables

E. No matter where the function is called, and no matter how it is called, its lexical scope is determined only by the location where it is Declared.

F. Lexical scopes look for a first-level identifier, such as a, b, and C. If Foo.bar.baz is referenced in your code, the lexical scope will only attempt to find the Foo identifier and then access the bar and Baz using the object property access Rules.

2. Deceptive morphology

We can sometimes use some statements to deceive the lexical scope, but one thing to be aware of: the spoofing scope can cause performance degradation

2.1 Eval ()

Eval () takes a string argument and regards the string as JavaScript execution

In strict mode, the code in Eval () has its own lexical scope, so the declaration in it cannot modify the effect of the extraterritorial code, whereas in Non-strict mode, the code in eval () can modify the scope of the Eval () method, which is all claims in the Eval () method and eval () method is in the same lexical scope, so you can modify the final Effect.

Similar to the new function (...), the function is dynamically generated for the passed-in string, so it is not elaborated too Much.

2.2 with ()

With (obj) {...} essentially creates or points to the lexical scope in obj, where all declarations are looked up in this scope when executed by the engine, and if not found, the previous scope of obj continues to be found, but A global variable is created when the global scope of the top level has not been found Yet. also, with in strict mode it is completely forbidden to Run.

2.3 Performance

Eval () and with () can spoof other lexical scopes by modifying or creating scopes at run Time. But it can greatly reduce the efficiency of the code, it is possible that the previous optimizations to the code are all invalid, making the code run SLOWLY.

You don't know. JavaScript (roll up) reading notes two----lexical scopes

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.