The standard should be called JavaScript LHS and RHS, which means “left and right of assignment operations” and does not mean “= left and right of assignment operators”. For example, console.log(a) performs RHS on A.
The variable LHS
In strict mode
Example 1
<! DOCTYPEhtml>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Left query of variables</title>
</head>
<body>Property query test</body>
<script>
'use strict'
var x = 'windowx'
function hello() {
x = 'hellox'; // Select * from x; // Select * from x
function hi() {
console.log(x); // The scope chain cannot be found.
}
hi()
}
hello()
</script>
</html>
Copy the code
Example 2
<! DOCTYPEhtml>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Left query of variables</title>
</head>
<body>Property query test</body>
<script>
'use strict'
function hello() {
x = 'hellox'; < span style = "max-width: 100%; clear: both; min-height: 1em;
}
hello()
</script>
</html>
Copy the code
Example 3
var x = 'hellox'
/ / equivalent to the
var x
x = 'hellox'
Copy the code
summary
In strict mode, LHS queries variables along the scope chain. An error will be thrown if the query cannot be found. Var x = ‘hellox’ is declared and left queried.
In non-strict mode
Example 1
<! DOCTYPEhtml>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Left query of variables</title>
</head>
<body>Property query test</body>
<script>
var x = 'windowx'
function hello() {
x = 'hellox'; // Hellox is under window
var y = 'helloy'; // helloy under hello
function hi() {
// Add the property to the last scope in the chain.
z = 'hiz' // hiz is under window
}
hi()
}
hello()
</script>
</html>
Copy the code
summary
Add the property to the last scope in the scope chain.
The variable RHS
Uncaught ReferenceError is thrown if the scope chain is not found in strict or non-strict mode.
Properties of the LHS
Example 1
<! DOCTYPEhtml>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Setting of properties</title>
</head>
<body>Setting of properties</body>
<script>
// 'use strict' // the output is' set was called ', whether commented or not
function hello() {}Object.defineProperty(hello.prototype, 'x', {
get: function () {
return 'getx';
},
set: function (value) {
console.log('Set' is called); }});var hi = new hello();
/** * The following code is the property set */
hi.x = 'hix'; Setters will be called if the property exists on the prototype chain with or without strict mode.
</script>
</html>
Copy the code
Example 2
<! DOCTYPEhtml>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Setting of properties</title>
</head>
<body>Setting of properties</body>
<script>
'use strict' // This code is currently running in strict mode, the console reported an error. If 'use strict' is commented out, the console does not report an error, and the JS engine ignores hi.y = 'hiy' automatically.
function hello() {}Object.defineProperty(hello.prototype, 'x', {
value: 'x'.writable: true});Object.defineProperty(hello.prototype, 'y', {
value: 'y'.writable: false
});
var hi = new hello();
/** * property set, query up the prototype chain */
hi.x = 'hix'; // If it is writable, then the assignment process is normal
hi.y = 'hiy'; // Cannot assign to read only property 'y' of object '#
' This code will be ignored if it runs in non-strict mode
</script>
</html>
Copy the code
summary
In strict mode, this property does not exist in the current scope chain. Assigning objct.x to a property with the same name on the prototype chain and writable:false will throw an error.
In non-strict mode, this property does not exist in the current scope chain. If the same property on the prototype chain is writable:false, then objct.x is assigned without throwing an error and the JS engine automatically ignores this assignment.
In either strict or non-strict mode, the property does not exist in the current scope. A setter with the same name exists on the stereotype chain and is called.
Properties of RHS
In both strict and non-strict mode, the prototype chain is searched and an exception is thrown if it is not found.
conclusion
Variable query | ||
---|---|---|
Strict mode | Nonstrict mode | |
Statement var x; | Add the property to the current scope | Identical strict model |
LHS x = value | Search in scope chain: failed to find, exception reported | If not found, add the attribute to the top-level scope of the search scope chain |
RHS x | Search in scope chain: no exception found | Identical strict model |
Attribute query | ||
---|---|---|
Strict mode | Nonstrict mode | |
LHS:objct.x = value .Object is queried using the variable LHS.X is property lookup.If there is still a ‘. ‘in the back, it is still according to the attribute to find |
This property does not currently exist in scope. If the same property writable:false is assigned to objct.x, an error will be thrown. | This property does not exist in the current scope. The property of the same name on the prototype chain is writable:false. The Js engine ignores this assignment statement. |
This property does not currently exist in the scope, there is a property of the same name on the stereotype chain, and a setter with the same name is called | Identical strict model | |
RHS:object.x .Object is the variable RHS.X is property lookup.If there is still a ‘. ‘in the back, it is still according to the attribute to find |
Prototype chain search, not found, reported an exception | Identical strict model |