current position:Home>Review of JavaScript knowledge points (XXIII) -- LHS reference and RHS reference

Review of JavaScript knowledge points (XXIII) -- LHS reference and RHS reference

2021-08-27 05:43:19 Baiqi ovo

This is my participation 8 The 23rd day of the yuegengwen challenge , Check out the activity details :8 Yuegengwen challenge

Compiler principle

JavaScript Usually classified as “ dynamic ” or “ Explain to perform ” Language , But it's actually a compiled language . But it is different from the traditional compilation language , It's not precompiled , The compiled results can not be transplanted in the distributed system . For all that ,JavaScript The compilation steps of the engine are very similar to those of traditional compilation languages . In some aspects, it may be more complicated than expected . In the process of traditional compiled language , A section of source code in a program goes through three steps before execution , Collectively referred to as “ compile ”:

  1. participle / Lexical analysis : Decompose a string of characters into ( For programming languages ) Meaningful code blocks , These blocks of code are called lexical units .
  2. analysis / Syntax analysis : Flow lexical units into ( Array ) Convert to a tree that represents the syntax structure of a program, which is composed of elements that are nested level by level ( Abstract syntax tree :AST).
  3. Code generation : take AST Convert to executable code .

JS Compiler principle

  • engine : To be responsible for the whole JavaScript Program compilation and execution process .
  • compiler : Responsible for syntax analysis and code generation .
  • Scope : Responsible for collecting and maintaining all variables ( identifier ) A series of queries , And implement a set of rules , Determine the current code execution access to these identifiers .

for example : The assignment of a variable performs two actions :

  1. The compiler declares a variable in the current scope ( If it has not been stated before )
  2. At run time , The engine looks for the variable in the scope , If it can be found, it will be assigned .

that , How does the engine find variables ?

LHS Query and RHS Inquire about

LHS(left-hand Side) Query and RHS(right-hand Side) Inquire about , It usually refers to the reference of the left and right sides of the assignment operation .

When the variable appears on the left side of the assignment operation LHS Inquire about ( Find the storage location and assign a value ), On the right RHS Inquire about ( Find its specific value )

LHS and RHS The meaning is “ Left or right side of assignment operation ” It doesn't necessarily mean “= The left or right side of the assignment operator ”.

There are several other forms of assignment , So conceptually it's best to understand it as “ Who is the target of the assignment operation (LHS)” as well as “ Who is the source of assignment operation (RHS)”

for instance :

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

    var c = foo(2);

 Copy code 

LHS Inquire about :

  1. c=... ,c Is the target of the assignment operation , So for c need LHS Inquire about .
  2. Hidden a=2( Implicit variable assignment ), Calling foo(2) When , You need to take the arguments 2 Assign a value to a formal parameter , So for a need LHS Inquire about
  3. b=.... , Same as 1

*RHS Inquire about

  1. foo(2) ,foo(2) On the right side of the assignment operation , Need to know foo(2) Value , So it is RHS Inquire about
  2. =a,a On the right side of the assignment operation , Need to know a Value , Yes a Conduct RHS Inquire about
  3. a..., stay return a+b in , Need to know a and b Value , Should be right a and b separately RHS Inquire about .

copyright notice
author[Baiqi ovo],Please bring the original link to reprint, thank you.

Random recommended