7

js中const的值什么时候可以修改

 2 years ago
source link: https://www.fly63.com/article/detial/12032
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

更新日期: 2022-08-24阅读: 131标签: 变量分享

扫一扫分享

"const声明的变量是个常量, 所以是不能修改的",重新定义const定义的数值的话,就会出现:Uncaught TypeError: Assignment to constant variable。实际上:

  1. 变量如果是基本类型? const定义的 "基本数据类型" 的变量确实不能修改
  2. 那引用数据类型呢?const定义的 "引用数据类型" 是可以的! (栽这了) 而且仅限于改变值

代码说明:

const columns = [
{
dataIndex: "id",
key: "id",
title: "流水号"
},
{
dataIndex: "no",
key: "no",
title: "订单号"
} ];

columns.forEach((r)=>r.align = 'center')//每个对象里面都添加一个align属性
//这样是可以改变的

//如果是单纯的修改定义的基本类型将会报错
const a = 2;//正确
a = 3;//再赋值就出错
const a;//也是错误的,必须初始化

const定义的基本类型不能改变,但是定义的对象是可以通过修改对象属性等方法来改变的。

链接: https://www.fly63.com/article/detial/12032


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK