当前位置:网站首页>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]);
}
})
}
边栏推荐
- Shell script - positional parameters (command line parameters)
- Common interview questions for embedded engineers 2-mcu_ STM32
- Can diffusion models be regarded as an autoencoder?
- 【pytorch】nn. AdaptiveMaxPool2d
- 【pytorch】nn. Crossentropyloss() and nn NLLLoss()
- 【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云、小程序、Arduino的温湿度监控系统
- 【pytorch】softmax函数
- 毕业季,我想对你说
- Record a redis timeout
- Shell script - definition, assignment and deletion of variables
猜你喜欢

I use flask to write the website "one"

树结构---二叉树2非递归遍历
![[pytorch] softmax function](/img/97/b8ae22e8496a77e665d716cb0e9ee3.png)
[pytorch] softmax function

Principles of Microcomputer - internal and external structure of microprocessor

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

Mysql 优化

安装Oracle EE

How to realize the usage of connecting multiple databases in idel

Implementation and application of queue
![[pytorch] 2.4 convolution function nn conv2d](/img/eb/382a00af5f88d5954f10ea76343d6e.png)
[pytorch] 2.4 convolution function nn conv2d
随机推荐
Input标签的type设置为number,去掉上下箭头
Shell script -select in loop
2.3 [pytorch] data preprocessing torchvision datasets. ImageFolder
Promise异步编程
Shell脚本-数组定义以及获取数组元素
2.4 激活函数
【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + DS18B20温度传感器 +NodeJs本地服务+ MySQL数据库
类加载
I use flask to write the website "one"
Learning practice: comprehensive application of cycle and branch structure (II)
[pytorch] 2.4 convolution function nn conv2d
Shell script case in statement
【pytorch】2.4 卷积函数 nn.conv2d
美团2022年机试
Principles of Microcomputer - Introduction
MapReduce编程基础
Daily office consumables management solution
delete和delete[]引发的问题
Daily practice of C language - day 80: currency change
[ESP nanny level tutorial] crazy completion chapter - Case: ws2812 light control system based on Alibaba cloud, applet and Arduino