当前位置:网站首页>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]);
}
})
}
边栏推荐
- 闭包实现迭代器效果
- 2.2 【pytorch】torchvision. transforms
- Redis——Lettuce连接redis集群
- Promise asynchronous programming
- Databinding source code analysis
- Jetson Nano 安装TensorFlow GPU及问题解决
- Why is the Ltd independent station a Web3.0 website!
- An overview of the design of royalties and service fees of mainstream NFT market platforms
- [ESP nanny level tutorial] crazy completion chapter - Case: ws2812 light control system based on Alibaba cloud, applet and Arduino
- Which method is good for the management of fixed assets of small and medium-sized enterprises?
猜你喜欢

Why is the Ltd independent station a Web3.0 website!

队列的实现和应用

MySQL optimization

Microcomputer principle - bus and its formation

How to launch circle of friends marketing and wechat group activities

Phishing identification app

Jeecg restart alarm 40001

Reproduced Xray - cve-2017-7921 (unauthorized access by Hikvision)

Ape anthropology topic 20 (the topic will be updated from time to time)

【pytorch】nn. Crossentropyloss() and nn NLLLoss()
随机推荐
安装Oracle EE
2.2 【pytorch】torchvision.transforms
Summary of reptile knowledge points
LogBack
Mise en œuvre simple de l'équilibrage de la charge par nacos
樹結構---二叉樹2非遞歸遍曆
如何高效拉齐团队认知
nacos简易实现负载均衡
[pytorch] softmax function
【pytorch】nn. Crossentropyloss() and nn NLLLoss()
Pain points and solutions of fixed assets management of group companies
Preparing for the Blue Bridge Cup -- bit operation
Shell script - array definition and getting array elements
[interview brush 101] linked list
2.3 [pytorch] data preprocessing torchvision datasets. ImageFolder
nacos服务配置和持久化配置
Leetcode daily question brushing record --540 A single element in an ordered array
Shell script -select in loop
Shell script case in and regular expressions
手指点击屏幕就模拟进入F11进入全屏