English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Before writing JavaScript code, we all know that we can use 'var' to define global variables and local variables, and we can also omit 'var', and it will not report an error in non-strict mode, but we did not know the difference between the two...
var x = 1; y = 4; console.log(x);//1 console.log(y);//4 console.log(window.x);//1 console.log(window.y);//4
A simple test can show that the defined x and y are mounted on the window object and become properties under window, which does not mean anything...
delete x; delete y; console.log(window.x);//1 console.log(window.y);//undefined
Let's take a look at the code above; the x property was not deleted, but y was deleted, and this is the difference
Why can variables defined with var not be deleted after definition?63;ECMAScript 5In the standard, object properties can be obtained using Object.getOwnPropertyDescriptor() to get the attribute information of a certain property of an object:
console.log(Object.getOwnPropertyDescriptor(window,"x")); console.log(Object.getOwnPropertyDescriptor(window,"y"));
The following information is obtained:
When defining without var, the default configurable is true, and operations such as delete can be performed, but when var defines a global variable, configurable becomes false, which means it cannot be deleted.
In addition, let's briefly talk about the issue of variable hoisting
alert(a);//Uncaught ReferenceError: a is not defined a = 100;alert(b);//undefined
var b = 200; Let's talk about the second piece of code, the global variable b declared with var in js will be hoisted, that is, var b = 200; is decomposed into var b;b=200; When parsing code, var b; is hoisted to the front, and a space is allocated in memory. Since b is not assigned, it defaults to undefined. In the first piece of code, when the js executes the alert() function, since no var declaration is made, the variable is not hoisted, there is no memory allocation, and an error is reported directly when alerting!
In ECMAScript6In the standard, an important concept is "JavaScript strict mode", which needs to be added at the beginning with "use strict";
Points to note about let:
1let has block scope; one {} is a scope
2let does not have variable hoisting within its scope
3let cannot be declared again within its scope (function scope and block scope)
First point: Block scope of let
Attention: The following code is executed in strict mode.
let n = 10; if(true){ let n = 50; } console.log(n);//10Indicates that the outer code block is not affected by the inner code block. If a variable n is defined using var, the output will be the modified one.50.
Second point: Variable hoisting issue
alert(a);//Uncaught ReferenceError: a is not defined let a = 100;
Different from var, let does not have variable hoisting. The above writing will directly report an error.
Third point: Repeated declaration issue
(function(){ let lTest = "let"; var vTest = "var"; let lTest = "let changed";//Uncaught SyntaxError: Identifier 'lTest' has already been declared var vTest = "var changed"; console.log(lTest); console.log(vTest); })();
let does not allow repeated variable declarations in the same scope. Otherwise, it will directly report an error!!!
const command
const is used to declare constants. Once declared, its value cannot be changed. If you must change the value of a variable, JavaScript will not report an error, but silently indicate failure (not working).
The scope of const is the same as let, only effective within the block scope where it is declared, and like let, it cannot be declared repeatedly.
Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, does not edit the content manually, and does not assume relevant legal liabilities. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)