当前位置:网站首页>ES6-const本质与完全不可改实现(Object.freeze)
ES6-const本质与完全不可改实现(Object.freeze)
2022-07-01 09:14:00 【su27_0101】
本质
const 是 es6 定义的 常量(定义后 不可改)标志
但不完全不可改
- 对于 基本类型(数字 字符串 布尔)不可以更改
- 因为 const保存的是其内容(或栈区地址 更准确)
- 但对于 复合类型(数组 对象)const 保存的是一个引用地址
- 只要引用地址不变可以任意修改该地址指向的对象的任意内部属性
const arr = [];
arr.push(3);// ok
arr = [];// error
但可以做到完全不能改
关键是利用Object.freeze
实现常量完全不可改
var constancize = (obj) => {
Object.freeze(obj);
Object.keys(obj).forEach((key, i)=>{
// 递归 对子属性为对象的 进行 freeze
if(typeof obj[key] == 'object'){
constancize(obj[key]);
}
})
}
边栏推荐
- js原型陷阱
- LogBack
- 【pytorch】transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
- Mysql8.0 learning record 17 -create table
- [interview brush 101] linked list
- 钓鱼识别app
- 【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + DS18B20温度传感器 +NodeJs本地服务+ MySQL数据库
- Win7 pyinstaller reports an error DLL load failed while importing after packaging exe_ Socket: parameter error
- TV size and viewing distance
- NiO zero copy
猜你喜欢

dsPIC30F6014a LCD 方块显示

Performance improvement 2-3 times! The second generation Kunlun core server of Baidu AI Cloud was launched

Installing Oracle EE

Ranking list of domestic databases in February, 2022: oceanbase regained the "three consecutive increases", and gaussdb is expected to achieve the largest increase this month

2.4 激活函数
![[video game training] real topic of 2013 video game of infrared optical communication device](/img/ef/c2c45c1c6c24aed0a4e93101047372.png)
[video game training] real topic of 2013 video game of infrared optical communication device

Mysql 优化

Structure de l'arbre - - - arbre binaire 2 traversée non récursive

jeecg 重启报40001

Learning practice: comprehensive application of cycle and branch structure (II)
随机推荐
Installing Oracle EE
Pain points and solutions of fixed assets management of group companies
The jar package embedded with SQLite database is deployed by changing directories on the same machine, and the newly added database records are gone
Ape anthropology topic 20 (the topic will be updated from time to time)
美团2022年机试
SDN_简单总结
[ESP nanny level tutorial] crazy completion chapter - Case: chemical environment system detection based on Alibaba cloud and Arduino, supporting nail robot alarm
Key points of NFT supervision and overseas policies
Common interview questions for embedded engineers 2-mcu_ STM32
[ESP nanny level tutorial] crazy completion chapter - Case: temperature and humidity monitoring system based on Alibaba cloud, applet and Arduino
Learning practice: comprehensive application of cycle and branch structure (II)
[ESP nanny level tutorial preview] crazy node JS server - Case: esp8266 + DS18B20 temperature sensor +nodejs local service + MySQL database
【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + DHT11 +NodeJs本地服务+ MySQL数据库
Promise异步编程
【电赛训练】红外光通信装置 2013年电赛真题
记一次redis超时
猿人学第20题(题目会不定时更新)
【pytorch】transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
如何高效拉齐团队认知
js valueOf 与 toString 区别