Original link: http://www.cnblogs.com/yangxiaoguai132/p/5064625.html
Recently in the process of learning JavaScript, contact with the concept of LHS and RHS, at the beginning of a little understanding, now do some carding, easy to understand later.
LHS and the Rhs:javascript engine have two lookup types, meaning the left and right of the assignment operation.
LHS: The LHS reference to which assignment is to be understood as the target of the assignment operation.
RHS: The value of which variable needs to be obtained, the value of which variable is RHS referenced, understood as the source of the assignment operation.
There are several forms of assignment operations, and for both of the above explanations of LHS and RHS, I have just come into contact with confusion.
My understanding is that the general need to be assigned on the left is the LHS reference, the right to find his value, that is, RHS reference.
For example:
1 function foo (a) {2 var b=a;3 rerurn a+b;4}5 var c=foo (2);
The above code has 3 LHS and 4 RHS, analyzed as follows:
First, C in Var C needs to be assigned to the left of the assignment operation, so C is LHS referenced
Second, the variable C needs to be assigned, his value is foo (2), then the value of foo (2), you need to find the value of foo (2), on the right side of the assignment operation, so the RHS reference to Foo (2)
Third, implicit assignment operation, pass 2 to function foo (a) {...} The parameter of the function A,a to the left of the assignment operation and a LHS reference to a
In the case of Var b=a, B needs to be assigned to the left of the assignment operation, so the lhs,b value of B will come from a, so where does the value of a on the right come from? This requires a RHS to the right of the assignment operation.
V, return a+b; in, you need to find the source of the values of A and B, both A and B are on the right side of the assignment operation to get the a+b value, so both A and B are RHS references.
Why distinguish between LHS and RHS?
Because the variable is not declared (it cannot be found in any scope), the two query behaviors are different.
For example:
1 function foo (a) {2 console.log (a+b); 3 b=a;4}5 foo (2);
The above code can not find the value of the variable when it is RHS, the Referenceerror exception will be thrown, if it is LHS cannot find the variable, in non-strict mode, in the global scope, a variable with that name, in strict mode, will throw an exception similar to RHS.
For example:
<! DOCTYPE html>
In the above code, LHS for B did not find the variable, a variable B with the same name was created in the global scope, and the B variable can be accessed outside of the function init.
If you change the code to the following:
1 <! DOCTYPE html> 2
A local variable b is defined in function, the global scope is defined, and the console error shows that there is no output in the window
Change the code to the following:
1 <! DOCTYPE html> 2
Change the code as shown above, the console does not have an error, because window.b is accessed as a property of window, so it returns undefined, and B is an error when it is not defined as a variable.
LHS and RHS in "turn" javascript