In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article, the editor introduces in detail "how to use LHS and RHS in JavaScript". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to use LHS and RHS in JavaScript" can help you solve your doubts.
Foreword:
For LHS and RHS, it literally means Left Hand Side and Right Hand Side, that is, the left-hand side and the right-hand side, which can generally be understood as the left and right side of the assignment operation. However, we cannot use the left side of the = sign and the right side to judge whether it is LHS or RHS.
LHS is an assignment operation, which can be seen as storing values in memory, while RHS is a value operation, which is retrieved from memory. In fact, there are several other forms of assignment operation, so conceptually it is best understood as "who is the target of the assignment operation (LHS)" and "who is the source of the assignment operation (RHS)".
Further understanding
These two different referencing methods treat undeclared variables differently, and this difference is beneficial for us to write code and analyze JS engine error reports. When performing a RHS query on a variable, if the lexical scope that traverses the variable fails to find the variable, the JS engine will throw a ReferenceError error. If the variable is successfully queried, but unreasonable operations are performed on the variable, such as performing a subscript value on a non-array variable, the JS engine will throw a TypeError error, even telling you that this operation can only correspond to an array. When executed on a variable
When querying LHS, the variable cannot be found after traversing the scope. In non-ES5 strict mode, the system will automatically create a variable with the same name in the global scope and transfer the reference to the newly created global variable. In ES5 strict mode, the JS engine throws the same ReferenceError error as RHS when the LHS query fails. ! [
Therefore, a careful distinction and understanding of LHS queries and RHS queries is beneficial both in terms of understanding the JS execution process itself and in analyzing errors.
The characteristics of JS language
JavaScript is usually classified as a "dynamic" or "interpreted execution" language in terms of type, but in fact it is a compiled language.
JavaScript is the most popular scripting language in the world, because all the web pages you browse on computers, phones, tablets, and countless HTML5-based mobile phones App, the interaction logic is driven by JavaScript.
In a nutshell, JavaScript is an interpreted programming language that runs in a browser.
However, this language is still a little different from the traditional compilation language, it is not compiled in advance, the compilation results can not be migrated on the distributed system, students who have done front-end project deployment, will also find that after we compile the project is a dist file, and then put the whole file directly on the web server, such as nginx, tomcat, etc., this is a very pure stand-alone deployment.
Compiling characteristics
For example, how much does our JavaScript engine do to execute an assignment statement?
Var girlfriend = 'naug'
In fact, after taking two steps, JavaScript will regard it as two statements: var girlfriend; and girlfriend = 'naug'
Definition declaration is made during the compilation phase
The assignment declaration is left in place waiting for the execution phase.
Analysis.
The assignment of a variable performs two actions: first, the compiler declares a variable in the current scope (if it has not been previously declared), and then at run time the engine looks for the variable in the scope and assigns a value to it if it can be found. LHS and RHS are the two kinds of search operations for variables mentioned above. The search process is assisted by scope (lexical scope) and executed in the second step of compilation. As we said earlier, LHS is an assignment operation that stores values in memory, while RHS is a value operation that can retrieve values from memory, so based on this information point, let's analyze a more complex example.
Function together (people) {var girlFriend = people;return girlFriend} var luckyGirl = together ('naug')
Q, how many LHS and RHS are used in this example? Answer, 3 LHS and 3 RHS
LHS
-people = 'naug' (implicit variable assignment) hidden in the function. When you call together (' naug'), you need to assign the argument naug to the formal parameter people, so you need to LHS the people.-for
GirlFriend = people, where girlFriend is on the left side of the assignment operation, that is, the variable stores the value in the memory interval of the scope space, that is, the LHS operation-
LuckyGirl =..., luckyGirl is on the left side of the assignment operation, and you need to store the value for this variable in memory, that is, LHS the luckyGirl.
RHS
-girlFriend = people, where people is on the right side of the assignment operation, and the javascript engine needs to perform a value operation on it, so do a RHS query-
Return girlFriend, because you need to know the value of girlfriend, do a RHS query to get the value of girlfriend-
LuckyGirl = together ('naug'), where together (' naug') is on the right side of the assignment operation, so you need to know the summary of the value after the function is executed: if the purpose of the search is to assign a variable, then the LHS query is used; if the purpose is to get the value of the variable, the RHS query is used.
The importance of distinguishing LHS from RHS
Because the two query behaviors are not the same when the variable has not been declared (the variable cannot be found in any scope). For the analysis of scope, you can take a look at this article about the scope in JS. What I want to say here is that both LHS and RHS will start in the current execution scope. If necessary (that is, they do not find the required identifier), they will continue to look for the target identifier to the superior scope, raise the scope one by one, and finally reach the global scope. Finally, whether it is found or not found, it will end here.
After reading this, the article "how to use LHS and RHS in JavaScript" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.