You don't know what the javascript-scope is

Source: Internet
Author: User

Scopes are a set of rules that determine where and how to find variables, functions, and so on (identifiers). If the purpose of the lookup is to assign a value to a variable, the LHS query is used, and if the purpose is to get the value of the variable, the RHS query is used.


The assignment operator causes the LHS query to be queried. = operator or an operation that passes in a parameter when the function is called will result in an assignment of the associated scope.


The JavaScript engine first compiles the code before it executes, and in this process a declaration like var a = 2 is decomposed into two separate steps:

  1. First, Var a declares a new variable in its scope. This is done at the very beginning, that is, before the code executes.
  2. Next, a = 2 queries (LHS query) variable A and assigns it a value.


LHS and RHS queries start in the current execution scope, and if necessary (that is, they do not find the required identifier), they continue to look for the target identifier to the ancestor scope, so that each ascent to the first-level scope (one floor), and finally to the global scope (top level), is stopped, whether found or not found.


An unsuccessful RHS reference causes the Referenceerror exception to be thrown. An unsuccessful LHS reference causes a global variable to be automatically created implicitly (in a non-strict mode) that uses the target referenced by LHS as an identifier, or throws a Referenceerror exception (in strict mode).

You don't know what the javascript-scope is

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.