Query mechanism --LHS of the RHS JavaScript

JavaScript engine to find a variable, there are two discovery mechanisms: LHS and RHS.

RHS query is simply to find the value of a variable, while the LHS is trying to find a variable container itself.

A simple example: when we execute when console.log (a), is performed RHS, because there is not a given any value. Accordingly, the need to find and get a value, so as to pass values ​​to conso.log (..).

And when we perform to a = 2, where LHS is a reference to a quote, because in fact we do not care what the current value is just trying to find a target = 2 this assignment.

 

have to be aware of is:

When we execute the following code:

function foo(a){

  console.log(a);  // 2

}

foo(2);

Here's the execution of the call to foo RHS references, but there is an implicit operation of a = 2, where they performed a LHS reference.

We usually function foo (a) {} into var foo, foo = function () {}, if so understood, will perform this function declaration LHS query, but when the engine executes the code, will not have a special thread the value for a function "is assigned to" foo. Therefore, the function declaration form to understand the assignment of LHS query is not appropriate.

Distinguish between LHS and RHS is a very important thing. If the RHS query variables in scope chain not find an exception is thrown ReferenceError of.

function foo(a){

  console.log(a + b);

}

foo()

The above code will throw an exception: b is not defined

In contrast, if the JavaScript engine executes the query LHS is, if the scope (global scope) up to the top of the target variables are not found, then it will target a variable name in the declaration have global scope, and return it to the engine. (Non-strict mode and strict mode disables the automatic creation or implicitly created a global variable)

function foo(a){
  b = a;  // b = 2
}
foo(2)
LHS above code executes queries in a non-strict mode, JavaScript engine is not found in the global scope b, so it declares a variable b in global scope. So in this case the result is not given to 2 and b is assigned.
 

Summary: The
scope is a set of rules for determining where and how to find variable (identifier). If the goal is to find a variable assignment, LHS will use the query; if the goal is to get the value of variables, will use the RHS query.

= Parameter passing operation will result in the assignment associated with the operation when the operator or the scope of the calling function.

LHS and RHS inquiry will begin in the current execution scope, if necessary (currently not found), will continue the search identifier (Scope chain) to the higher scope.
RHS will cause an unsuccessful ReferenceError exception. Unsuccessful LHS automatically implicitly create a global variable (non-strict mode), the target variable is used as an identifier LHS cited in the global scope. (If strict mode also throw ReferenceError exception).

Guess you like

Origin www.cnblogs.com/Soaring-Free/p/11361143.html