"JavaScript You Don't Know"-LHS and RHS

"JavaScript You Don't Know"-Chapter 1 Notes
Note Directory:"JavaScript You Don't Know" Note Directory-CSDN Blog

LHS and RHS

The JS code is as follows:

var a = 2

At compile time, the compiler will do the following:

1. When encountering var a, the compiler will ask the scope whether there is already a variable with this name in the collection of the same scope. If so, the compiler ignores the declaration and continues compilation; otherwise it asks the scope to declare a new variable named a in the current scope's collection.
2. Next, the compiler will generate the runtime code required for the engine, which is used to handle the assignment operation a = 2. When the engine runs, it will first ask the scope whether there is a variable called a in the current scope collection. If yes, the engine will use this variable; if no, the engine will continue to look for the variable.

Here, when looking for variable a, the LHS query is used. In addition to LHS, there are alsoRHSqueries.

LHS query is performed when the variable appears on the left side of the assignment operation, otherwise RHS query is performed

A RHS query is indistinguishable from simply finding the value of a variable, whereas an LHS query attempts to find the variable's container itself so that Assign a value to it.

for example:

console.log( a );

The reference to a here is a RHS reference because a is not assigned any value. Accordingly, the value of a needs to be found and obtained so that the value can be passed to console.log(…)

a = 2

The reference to a here is theLHS reference, because in fact we don’t care what the current value is, just wants to find a target for the = 2 assignment operation.

Distinguish between LHS and RHS

Why the distinction? because:In the case where the variable has not been declared (the variable cannot be found in any scope), the behavior of the two queries is different.

IfRHS the query cannot find the required variables in all nested scopes, the engine will throwReferenceErrorException.

In contrast, when the engine executesLHS query, if the target variable cannot be found in the top-level (global scope), And if the program is running in non-"strict mode", a variable with this name will be created in the global scope and returned to the engine.

Notice:Strict mode prohibits automatic or implicit creation of global variables. Therefore, when the LHS query fails in strict mode, a global variable will not be created and returned, and the engine will throw a similar ReferenceError exception as when the RHS query fails.

If the RHS query finds a variable, but performs unreasonable operations on the value of this variable, such as trying to operate on a non-function type If you make a function call with a value of type null or undefined, or refer to a property with a value of type null or undefined, the engine will throw another type of exception called . TypeError

ReferenceError is related to scope determination failure, while TypeError means that scope determination is successful, but the operation on the result is illegal or unreasonable.

Summary of LHS and RHS

Scope is a set of rules that determine where and how variables (identifiers) are found.If the purpose of the search is to assign a value to a variable, the LHS query will be used; if the purpose is to obtain the value of the variable, the RHS query will be used.

Assignment operators cause LHS queries.The = operator or the operation of passing parameters when calling a function will result in an assignment operation in the associated scope.

The JavaScript engine first compiles the code before it is executed. During this process, a statement like var a = 2 is broken down into two separate steps:

  1. First, var a declares the new variable in its scope. This happens at the very beginning, before the code is executed.
  2. Next, a = 2 queries (LHS queries) the variable a and assigns it a value.

Both LHS and RHS queries start in the current execution scope. If they do not find the required identifier, they continue to search for the target identifier in the upper scope, each time going up one scope, and finally reach the global scope (top level ), it will stop regardless of whether it is found or not found.

Unsuccessful RHS references cause a ReferenceError exception to be thrown. An unsuccessful LHS reference results in the automatic and implicit creation of a global variable (in non-strict mode) that uses the target of the LHS reference as an identifier, or throws a ReferenceError exception (in strict mode)

Guess you like

Origin blog.csdn.net/karshey/article/details/134648516