Home php教程 PHP开发 An in-depth explanation of ES6 let and const commands

An in-depth explanation of ES6 let and const commands

Dec 29, 2016 pm 01:20 PM

Variables declared by let and const are only valid within the code block

{
let a = 10;
var b = 1;
}
a // ReferenceError: a is not defined.
b // 1
Copy after login

There is no variable promotion

Variables must be declared after Use, otherwise an error will be reported

var tmp = 123;
if (true) {
tmp = 'abc'; // ReferenceError
let tmp;
}
Copy after login

Duplicate declaration is not allowed

// 报错
function () {
let a = 10;
var a = 1;
}
Copy after login

Block-level scope

function f() { console.log('I am outside!'); }
(function () {
if(false) {
// 重复声明一次函数f
function f() { console.log('I am inside!'); }
}
f();
}());
//I am inside! ES5 函数提升
//I am outside! ES6 块级作用域
Copy after login

const command

Declare a read-only constant. Once declared, the value of the constant cannot be changed

Once a variable is declared, it must be initialized immediately and cannot be left for later assignment

Global variables declared by the let command, const command, and class command are not attributes of the global object

var a = 1;
// 如果在Node的REPL环境,可以写成global.a
// 或者采用通用方法,写成this.a
window.a // 1
let b = 1;
window.b // undefined
Copy after login

Now I will introduce to you the const command of ES6 separately

JS with ecma as the core has always had no concept of constants, and es6 has made up for this flaw. ;

const foo='foo';
foo='bar';//TypeError: Assignment to constant variable.
Copy after login

The above example declares a basic type constant. If you try to modify the initial value, an error will be reported; if it is a reference type value, the same applies, but There is one thing to note. Here is an example:

const foo=[];  
foo=[1];//Assignment to constant variable.
Copy after login

Normal error reporting, nothing wrong, look again:

const foo=[1,2,3];
foo[1]=4;
console.log(foo)//[1, 4, 3]
Copy after login

How come there is no error? And can the modification be successful? The difference between these two examples is that the former has modified the pointer (you need to be familiar with js reference types) and the corresponding content has changed, while the latter does not point to the same but the content of the object has changed. For foo, I just A pointer is responsible for pointing to the corresponding object. As for the content of the object, it doesn’t matter to me, so it can be modified; if you don’t want the content to change, you can use another method;

const foo=Object.freeze([1,2,3]);
foo[1]=4;
console.log(foo)//[1, 2, 3]
Copy after login

So you don’t have to worry about being modified;

For more in-depth articles on let and const commands in ES6, please pay attention to the PHP Chinese website!


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)