ES6 keyword const

Today let's talk about another keyword const of ES6

1. const keyword

Const is the abbreviation of constant (constant). const and let are also used to declare variables, but const is used to declare a constant. Since it is a constant, its value cannot be changed. It's not like the variables declared with var before can be changed casually.
It can be seen that when you encounter const, it is time to change your wayward code!

2. Talk about constants

1. Unchangeable
const Name='张';
Name = 'zhang' // 错误,不可修改常量 Name
2. Only works in block-level scope, which is the same as let
if (x) {
   const Name='张' 
}
alert(Name) // 错误,在代码块{}外,Name 不起作用 
3. There is no variable promotion, it must be declared before use, which is the same as let
if (x) {
   alert(Name) // 错误,使用前未声明
   const Name='张' 
   
}

4. The same variable cannot be declared repeatedly, and it is the same as let
var Name='张' ;
const Name='张' // 错误 变量已经声明 ,不可重复声明
5. You must assign a value after the declaration
const NAME; // 错误 声明后没赋值

Three, const declaration object

Look at the code first
const Person = {
    'name':'张'
} 
Person.name='王';
Person.age=20;

console.log(Person) // 输出 {'name':'王','age':20 }
Isn't it said that good constants cannot be modified. do something...
Calm down and listen to me!
Here is a concept: assignment by reference
In the assignment process, we are divided into assignment by value and assignment by address. Here we will talk about assignment assignment by address.
Pass-by-Address: During the assignment process, the variable stores the address of the data, not the original data or a copy of the data.
I don't quite understand, it's okay, look at the code first
var person1={'name':'张'}
var person2=person1
person2.name='王'
console.log(person1) // 输出 {name:'王'}
console.log(person2) // 输出 {name:'王'}
Why does the name of person2 also change after the name of person2 is changed? This is assignment by reference
For example: For example, you made an appointment with a hair dyer, Xiao Wang, and Xiao Wang came to your home along the address you gave to dye your hair in grandma gray.
A few days later, you feel that he can't dye his hair well and his hair color is not good-looking, so you make an appointment with Xiao Li. Xiao Li also comes to your house along the address, and improves your hair and dyes it into flax.
Then no matter who comes to your house, the color of your hair must be flax if you come to your address, because the last time you dyed your hair was flax.
look at the code
// 小王给你染得奶奶灰
var wang={hair:'奶奶灰'}

// 过了几天,你让小李来,并告诉了他地址
var li=wang

// 小李给你染得亚麻色
var li={hair:'亚麻色'}

// 那么不管谁来你家,你的发色一定是亚麻色
console.log(wang) // 输出 {'hair':'亚麻色'}
console.log(li) //  输出 {'hair':'亚麻色'}
Turning to the code at the beginning, the structure is the same, you should understand it.
Back to our keyword const, using const to declare a constant of an object type is assignment by reference. And the unmodifiable is the address of the object in memory, not the object itself (the address of your home that does not change, not your hair color)
So the above code can be output normally when consoled
var person1={'name':'张'}
var person2=person1
person2.name='王'
console.log(person1) // 正常输出 {name:'王'}
console.log(person2) // 正常输出 {name:'王'}
However, if you write like this, you will get an error
var person1={'name':'张'}
person1={} // 错误,给常量person1 重新赋值(即改变你家地址)

summary:

const is used to declare a constant and must be assigned a value. It cannot be modified after declaration. Like let, it only works in the block-level scope. The same variable cannot be declared repeatedly. There will be no variable promotion. When , pay attention to assignment by reference.

ok!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325436401&siteId=291194637