当前位置:网站首页>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.3 [pytorch] data preprocessing torchvision datasets. ImageFolder
- nacos服务配置和持久化配置
- Shell script - string
- 通过 代码实例 理解 浅复制 与 深复制
- [ESP nanny level tutorial preview] crazy node JS server - Case: esp8266 + MQ Series + nodejs local service + MySQL storage
- [ESP nanny level tutorial preview] crazy node JS server - Case: esp8266 + DHT11 +nodejs local service + MySQL database
- Shell脚本-变量的定义、赋值和删除
- Promise异步编程
- 【pytorch】nn.CrossEntropyLoss() 与 nn.NLLLoss()
- 2.2 【pytorch】torchvision. transforms
猜你喜欢

How to solve the problem of fixed assets management and inventory?

Jetson nano installs tensorflow GPU and problem solving

How to manage fixed assets efficiently in one stop?

2.4 activation function

Principles of Microcomputer - Introduction

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

2.4 激活函数

nacos服务配置和持久化配置

Preparing for the Blue Bridge Cup -- bit operation

2.2 【pytorch】torchvision. transforms
随机推荐
Pain points and solutions of equipment management in large factories
LogBack
JCL 和 SLF4J
js valueOf 与 toString 区别
Promise asynchronous programming
nacos簡易實現負載均衡
猿人学第20题(题目会不定时更新)
[ESP nanny level tutorial] crazy completion chapter - Case: ws2812 light control system based on Alibaba cloud, applet and Arduino
2.2 【pytorch】torchvision.transforms
毕业季,我想对你说
In the middle of the year, where should fixed asset management go?
Niuke monthly race 22- collect pieces of paper
安装Oracle EE
Shell脚本-数组定义以及获取数组元素
美团2022年机试
Common interview questions for embedded engineers 2-mcu_ STM32
NiO zero copy
【pytorch】nn.AdaptiveMaxPool2d
Tree structure -- binary tree 2 non recursive traversal
Input标签的type设置为number,去掉上下箭头