当前位置:网站首页>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]);
}
})
}
边栏推荐
- 【pytorch】2.4 卷积函数 nn.conv2d
- Day06 branch structure and cycle (III)
- [video game training] real topic of 2013 video game of infrared optical communication device
- Shell script -for loop and for int loop
- The fixed assets management system enables enterprises to dynamically master assets
- SDN_简单总结
- Databinding source code analysis
- How to effectively align team cognition
- laravel postman 提交表单出现419错误。2020年7月6日记。
- js 使用toString 区分Object、Array
猜你喜欢
![[pytorch] softmax function](/img/97/b8ae22e8496a77e665d716cb0e9ee3.png)
[pytorch] softmax function

Learning practice: comprehensive application of cycle and branch structure (II)

Mise en œuvre simple de l'équilibrage de la charge par nacos

钓鱼识别app

Pain points and solutions of fixed assets management of group companies

Understanding and implementation of AVL tree

2.2 【pytorch】torchvision.transforms

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

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.3 [kaggle dataset - dog feed example] data preprocessing, rewriting dataset, dataloader reading data](/img/6e/d8ef618127ac492f5142f7b600266d.png)
2.3 [kaggle dataset - dog feed example] data preprocessing, rewriting dataset, dataloader reading data
随机推荐
Tree structure -- binary tree 2 non recursive traversal
[ESP nanny level tutorial preview] crazy node JS server - Case: esp8266 + DS18B20 temperature sensor +nodejs local service + MySQL database
计网01-物理层
【pytorch】nn. AdaptiveMaxPool2d
Bird recognition app
laravel postman 提交表单出现419错误。2020年7月6日记。
It technology ebook collection
How to solve the problem of fixed assets management and inventory?
R language observation log (part24) -- initialization settings
序列化、监听、自定义注解
Ape anthropology topic 20 (the topic will be updated from time to time)
Microcomputer principle - bus and its formation
Naoqi robot summary 28
Daily office consumables management solution
2.2 【pytorch】torchvision. transforms
Shell脚本-特殊变量:Shell $#、$*、[email protected]、$?、$$
Log4j log framework
2.4 activation function
猿人学第20题(题目会不定时更新)
Phishing identification app