JavaScript 修改常数
示例
声明变量const只能防止其值被新值替换。const对对象的内部状态没有任何限制。下例显示,const可以更改对象的属性值,甚至可以添加新的属性,因为分配给对象的对象person是修改的,但不能替换。
const person = {
name: "John"
};
console.log('The name of the person is', person.name);
person.name = "Steve";
console.log('The name of the person is', person.name);
person.surname = "Fox";
console.log('The name of the person is', person.name, 'and the surname is', person.surname);结果:
The name of the person is John The name of the person is Steve The name of the person is Steve and the surname is Fox
在此示例中,我们创建了名为的常量对象,person并重新分配了person.name属性并创建了新person.surname属性。
热门推荐
6 家长会简短祝福语
10 精辟简短的公司祝福语
11 毕业写给学校简短祝福语
12 同事孩子上学祝福语简短
13 女神节祝福语简短创意
14 冬至宾馆祝福语简短独特
15 元旦祝福语的英文简短
16 晚安好梦祝福语简短
17 小托班祝福语简短
18 结婚祝福语简短大气古风