You don't know. JavaScript (1) LHS queries and RHS queries

Source: Internet
Author: User

Plan to sort out the knowledge in JavaScript you don't know, write your own ideas, and urge yourself to read.

Let's start with an overall introduction, and we'll give you a comprehensive example at the end.

RHS query and simply find the value of a variable indistinguishable, and LHS query is to try to find the variable container itself, so that it can be assigned a value.

LHS Query

The LHS query refers to the container itself where the variable is found, so that it can be assigned a value. That is, the target of the assignment operation is found.

The LHS query will be queried along the scope chain, and if found, the value will be assigned to the variable, and if it is still not found at the top of the scope, the variable will be created at the top of the scope chain.

As an example,

var a=2;

var a=2; equivalent to var A; a=2;

Here's A is a LHS reference, we just want to find an assignment target for 2, not to care about the target (here is a) what is the value of something.

Because Var A; has already added a to the current scope, so when LHS queries A, it finds a, that is, the target of the assignment operation.

If there is no Var a, then when LHS queries a, it will not find a, it will create the variable a in the scope.

RHS Query

The RHS query is the value of the normal query variable, that is, the value of the variable.

The RHS query will be queried along the scope chain, and if found, the value will be obtained and returned, and if it is still not found at the top of the scope, an error will be thrown (such as TypeError, Referenceerror).

As an example,

Console.log (a);

Here A is a RHS reference, because Console.log needs to get the value of a to output a value.

Of course the console.log here is also a RHS reference, where the console object is RHS queried, and the resulting value is checked for a method called log.

The A in the example will throw an error because it has not been declared.

General examples
(function  Test () {    a=2;}); Console.log (a);

The test function in the example is not executed, so a=2 is not executed, and there is no LHS query, there is no variable a.

In Console.log (a), when a RHS query is made to a, a is found along the scope chain, and a is missing, so an error (Referenceerror) is thrown.

However, if executed immediately, no errors will be thrown.

(function  Test () {    a=2;}) (); Console.log (a);

The function of parentheses here is to execute functions immediately, so the assignment statement a=2 executes, so a LHS query is made along the scope chain for a.

LHS query cannot find a, create variable A at the top of the scope chain (global scope).

Therefore Console.log (a) in a RHS query, along the scope chain lookup can be found in the global scope of a, you can get the value of a.

Of course, the above iife (immediate execution function) can also be written as another situation, executing the same result.

(function  Test () {    a=2;} ()); Console.log (a);

Understanding LHS and RHS queries is critical to understanding scopes and can help us solve many bugs.

You don't know. JavaScript (1) LHS queries and RHS queries

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.