JavaScript LHS and RHS Search

I read Scopes and Closure of You, I don’t know JS books by Kyle Simpson, in particular this section Speak Compiler .

There they mention the search for LHS and RHS . I do not understand these two terms, can someone help me implement them?

+15
javascript lookup
source share
5 answers

LHS searches are performed when a variable appears on the left side of an assignment operation, and RHS searches are performed when a variable appears on the right side of an assignment operation.

I think of it as follows:
Lhs search - container search
Search rhs is a search for value

+10
source share

I think of it this way: lhs search is a container search rhs search is a value search

I like that Kyle Simpson is very suitable, but this specific explanation in the nutshell made the house loud and clear to me.

There is always a trade between “just say what I need to know, nothing more” and drilling to better understand it at a deep level.

Sometimes such a deep understanding can help you avoid trouble, debug, write test code, optimize and refactor.

Currently, I read and watch a lot of Kyle's writings and online training, he has the ability to explain well. Many instructors lose people along the way, too quickly, because they have experience, and it is difficult for them to slow down - on the other hand, if you are too solid, the conversation becomes uninteresting, and you just tune in.

+4
source share

LHS - Look for an identifier to assign goals or to assign a value to it.

let foo; // It looking for foo identifier in global scope and assign value // So it an LHS foo = 1; // It also LHS as it assigning new value foo = 2; 

Now, RHS - this means that when you look for an identifier to use it (not assign a value)

 function foo() { alert(2); } // this look for an identifier called foo and // in global scope it a function decoration // and execute it foo(); 
+3
source share

A simple example from the same book you mentioned

 function foo(a) { console.log( a ); // 2 } foo( 2 ); 

LHS : When you pass the value (2) to the foo method, the compiler sets the parameter to a = 2 , which is called LHS Lookup. It will simply find the container variable to assign the value to.

RHS : To run console.log to print a , you need an RHS link for the value of a. This is called RHS Lookup.

Another example

 function foo(a) { var b = a; return a + b; } var c = foo( 2 ); 

3 LHS from the above example - **

  1. c = (container for storing the return of the foo method)
  2. a = 2 (when you pass the value 2 to the method, the compiler will assign a = 2)
  3. b =

4 RHS from the above code snippet

  1. foo (2) - need a reference to a to get the value
  2. = a - to get the value of b, you need a link to a
  3. a - to get the value of a, you need a link to
  4. b - to get the value of b, you need a link to b

Edit:

+3
source share

Do not think of it as an assignment on the left or on the right, think of it as storing a value in memory and retrieving it later.

For example, when you type b in the Chrome Developer Console, it starts an RHS search (gets the value b ), and if the value is not found, it raises a ReferenceError .

In contrast, when you type b = 2 in the Chrome Developer Console, it starts an LHS search, and if b is not found in the nested scope, the JS compiler declares it in the global scope (depending on whether you are executing the code in strict mode or not )

For example, consider the following code.

 function foo(a) { console.log( a + b) } foo( 2 ); 

When the JS compiler executes the code, it first searches for the function foo and determines if it is declared in the current area (the hosting environment is here), which is called RHS lookup. Now, in the area foo, the argument will be 2, and when we declared the function foo(a) when we wrote foo( 2 ) we implicitly assign the value 2 a or a = 2 , which is called the LHS lookup (assignment 2 a ), now rewind forward. The compiler will again go to the console.log( a + b) line console.log( a + b) will again search for the values ​​of a and b (again search for RHS ) and, if the values ​​are found, it will assign it to console.log argument (if you assume that console.log defined as console.log(arg1) in the hosting environment, then arg1 = value of a+b (which again is an LHS search).

In short:

• when the mechanism receives the value of the console.log(b) variable, it receives the value from memory b . This is an RHS search

• when the mechanism assigns the value of the variable b = 2 , it looks for the value of b in the scope - if it is found, it sets the value 2 in the memory location b , if not, it looks at the scope of the top level, This is an LHS search.

+2
source share

All Articles