As we all know, JavaScriptVariables are searched according to the scope chain (for knowledge about scope and scope chain, please refer to my other article, "Based on "Performance Tuning of JavaScript Scope Chain"), then, for a simple assignment operation, are the search methods for variables on the left and right sides of the equal sign the same? Let's start with a simple example~
console.log(a); // undefined var a = 3; console.log(a); // 3 console.log(b); // ReferenceError b = 4; console.log(b); //4
The concept is as follows:
LHSQuery: Trying to find the variable container itself so that it can be assigned a value
RHS query: Find the value of a variable
For an assignment statementvar a = b;
, perform LHS query on the left side of the equal sign, RHS query is performed on the right side of the equal sign; if it is an ordinary print statement console.log(a)
, then the search variable a
belongs to the RHS query.
The similarities between the two: both follow the scope chain to find .
(1) LHS query
When the JavaScript engine executes the LHS query, if the target variable cannot be found in the top-level scope, then, it will Create a variable with that name in the global scope and return it to the engine (in non-strict mode).
Please note that the global variables created in this way are not, strictly speaking, real variables, but attributes of the global object , which can be passed <a href="http://www.php.cn/wiki/1298.html" target="_blank">delete</a>
operator will delete it. However, global variables declared with var
cannot be deleted using the delete
operator.
Refer to the example at the beginning of the article:
b = 4; console.log(b); // 4 delete b; console.log(window.b); // undefined
There is no variable declared in the program b
, but since the LHS query will automatically create the target variable that is not found, print b
Return 4. Then delete b, and the deletion will be successful.
If it is strict mode:
"use strict"; b = 4; console.log(b);
At this time, the LHS query will not be able to automatically create undeclared target variables, so throws an exception when printing
b: Uncaught ReferenceError: b is not defined
.
(2) RHS query
When the JavaScript engine executes the RHS query, if the target variable cannot be found in the scope chain, then the engine will throw a ReferenceError exception.
Refer to the example at the beginning of the article:
console.log(b); b = 4;
RHS query variableb
, the variable definition was not found in the global scope, so the engine threw an exceptionUncaught ReferenceError: b is not defined
(1) Variable promotion
Concept: Variables declared with var will always be hidden by the JavaScript interpreter "Promoted" to the top of the method body.
Refer to the example at the beginning of the article:
console.log(a); var a = 3;
The JavaScript engine will parse it as:
var a = undefined; console.log(a); a = 3;
So, the first time a
is printed, # will be returned. ##undefined.
ReferenceError represents the failure of scope determination, that is, the failure to query variables within the scope.
TypeError means that the scope determination is successful, but the operation on the result is illegal or unreasonable.
foo(); var foo = function () { console.log('a'); }
foo() statement, first RHS search, find the
foo variable in the global scope, the value is
undefined (variable promotion).
Then operate the
foo variable in the function execution mode. Obviously,
undefined is not a legal function, so the engine throws an exception:
Uncaught TypeError: foo is not a function, execution failed.
The above is the detailed content of Variable lookup in JavaScript. For more information, please follow other related articles on the PHP Chinese website!