当前位置:网站首页>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]);
}
})
}
边栏推荐
- 小鸟识别APP
- 微信小程序 webview 禁止页面滚动,同时又不影响业务内overflow的滚动的实现方式
- Set the type of the input tag to number, and remove the up and down arrows
- nacos服务配置和持久化配置
- Meituan machine test in 2022
- 【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云、小程序、Arduino的温湿度监控系统
- 【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云、小程序、Arduino的WS2812灯控系统
- pcl_viewer命令
- Promise asynchronous programming
- 钓鱼识别app
猜你喜欢
足球篮球体育比赛比分直播平台源码/app开发建设项目
Principle and application of single chip microcomputer timer, serial communication and interrupt system
Implementation and application of queue
【pytorch】nn. Crossentropyloss() and nn NLLLoss()
【pytorch】nn.CrossEntropyLoss() 与 nn.NLLLoss()
nacos服务配置和持久化配置
Jetson Nano 安装TensorFlow GPU及问题解决
Reproduced Xray - cve-2017-7921 (unauthorized access by Hikvision)
[video game training] real topic of 2013 video game of infrared optical communication device
【电赛训练】红外光通信装置 2013年电赛真题
随机推荐
2.4 activation function
Pain points and solutions of equipment management in large factories
Tree structure --- binary tree 1
Structure de l'arbre - - - arbre binaire 2 traversée non récursive
毕业季,我想对你说
Summary of reptile knowledge points
Principle and application of single chip microcomputer timer, serial communication and interrupt system
Daily office consumables management solution
闭包实现迭代器效果
I use flask to write the website "one"
【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + DS18B20温度传感器 +NodeJs本地服务+ MySQL数据库
如何高效拉齐团队认知
Shell脚本-变量的定义、赋值和删除
Why is the Ltd independent station a Web3.0 website!
【pytorch】transforms. Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
Phishing identification app
Shell script -select in loop
Shell script - positional parameters (command line parameters)
美团2022年机试
Jetson Nano 安装TensorFlow GPU及问题解决