Home > Web Front-end > JS Tutorial > body text

Variable lookup in JavaScript

PHPz
Release: 2017-04-04 10:30:14
Original
2034 people have browsed it

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
Copy after login

1. LHS (left-hand-side left search) and RHS (right-hand-side right search) )

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 .

2. The difference between LHS and RHS query

(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
Copy after login

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 bReturn 4. Then delete b, and the deletion will be successful.

If it is strict mode:

"use strict";
b = 4;
console.log(b);
Copy after login

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;
Copy after login

RHS query variableb, the variable definition was not found in the global scope, so the engine threw an exceptionUncaught ReferenceError: b is not defined

3. Tips

(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;
Copy after login

The JavaScript engine will parse it as:

var a = undefined;
console.log(a); 
a = 3;
Copy after login

So, the first time a is printed, # will be returned. ##undefined.

(2) ReferenceError and TypeError

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.

For example:

foo();
var foo = function () {
     console.log('a');
}
Copy after login
When executing the

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!