当前位置:网站首页>Object.freeze() learning
Object.freeze() learning
2022-07-30 01:25:00 【Hemubai】
前言
Object.freeze()方法可以冻结一个对象,The object is after freezing:No new properties can be added to this object,Cannot modify the original property value,不能删除已有属性,不能修改已有属性的可枚举性、可配置性、可写性.
一、基本使用
normal object operation
var obj = {
name:'小米',age:25,address:'地址'};
obj.sex = '男'; //添加属性sex
obj.age = 26; //修改age的值
delete obj.address; //删除属性address
console.log(obj); // {age:26,name:'小米',sex:"男"}
使用Object.freeze()冻结对象
var obj = {
name:'小米',age:25,address:'地址'};
obj.__proto__.title = '原型';
Object.freeze(obj); //冻结对象obj
obj.age = 30; //修改原有属性
console.log(obj,'1');
//{name: "小米", age: 25, address: "地址"} "1" //不能修改成功
obj.sex = '男'; //添加新的属性
console.log(obj,'2');
//{name: "小米", age: 25, address: "地址"} "2" //不能添加新属性
delete obj.address; //删除已有属性
console.log(obj,'3');
//{name: "小米", age: 25, address: "地址"} "3" //不能删除已有属性
Object.defineProperty(obj,'age',{
enumerable:false,
configurable:false,
writable:true
}) //不能修改已有属性的可枚举性、可配置性、可写性
console.log(obj,'4') //Cannot redefine property: age
Object.isFrozen()判断对象是否被冻结
var data = {
name:'小小',title:'信息'};
var newObj = Object.freeze(data);
console.log(newObj === data,'Judging whether the object is consistent'); //true
console.log(Object.isFrozen(data),'判读data是否被冻结') //true
console.log(Object.isFrozen(newObj),'判读newObj是否被冻结')//true
var newData = {
name:'pear pear'}
console.log(Object.isFrozen(newData),'判读newData是否被冻结')//false
冻结数组
var arr = [1,23,34,2];
Object.freeze(arr);
arr[0] = '修改';
console.log(arr);
//Identifier 'arr' has already been declared
二、Shallow Freeze and Deep Freeze
1.浅冻结
如上所说,Object.freeze()Simple objects can be frozen,如果对象里还有对象,Then frozen still valid?
var obj={
name:'小米',info:{
title:'新闻',value:100}};
Object.freeze(obj);
obj.name='mm';
console.log(obj);
//{name:'小米',info:{title:'新闻',value:100}}
obj.info.value = 200;
console.log(obj);
//{name:'小米',info:{title:'新闻',value:200}}
以上所示,When object attributes as object,Object.freeze()失效了.
Object.freeze()Only shallow copy is supported.
2.深冻结
function deepFreeze(obj){
//Get object property name
let names = Object.getOwnPropertyNames(obj);
names.forEach(item =>{
let data = obj[item];
if(data instanceof Object && data !== null){
deepFreeze(data)
}
})
return Object.freeze(obj)
}
let dataAll = {
name:'小米',info:{
value:120,title:'xxx'}};
deepFreeze(dataAll);
dataAll.info.value = 200;
console.log(dataAll);
//{name:'小米',info:{value:120,title:'xxx'}}
三、Object.freeze()原理
Object.freeze()Mainly used in the following two methods:
Object.definedProperty():Can define whether the properties of an object can be deleted,修改等.
Object.seal():Can prevent adding new properties,不能修改.
Object.defineProperty(person, 'name', {
configurable: false,// 表示能否通过delete删除属性,能否修改属性的特性...
enumerable: false,// Indicates whether it can be enumerated.直接在对象上定义的属性,基本默认true
writable: false,// 表示能否修改属性的值.直接在对象上定义的属性,基本默认true
value: 'xm'// 表示属性的值.When accessing a property read from here,修改属性时,也保存在这里.
})
总结
If the object has a lot of content,static data,and will not modify it,那可以用Object.freeze()冻结起来.This can greatly improve performance,The effect of the improvement increases as the amount of data increases.对于纯展示的大数据,都可以使用Object.freeze提升性能.
在vue项目中,data初始化 There are usually many variables in,If you don't want to use it later,可以使用Object.freeze().这样可以避免vue初始化时候,do something useless,从而提高性能.
data(){
return{
list:Object.freeze({
'I don't need to change'})
}
}
Refer to the original blogger:文章地址
边栏推荐
- npm ERR! code ENOTSUPnpm ERR! notsup Unsupported engine for [email protected]: wanted: {“n
- Reconstruction of binary tree
- SSM整合案例
- 小白必看|不用编程的labview,ATECLOUD完全满足你的需求
- Nacos配置中心用法详细介绍
- Detailed explanation of nacos cluster configuration
- Vmtouch - under Linux file cache management artifact
- 自学HarmonyOS应用开发(56)- 用Service保证应用在后台持续运行
- 专心致志做事情
- 面试题:手写Promise
猜你喜欢
新型LaaS协议Elephant Swap给ePLATO提供可持续溢价空间
「MySQL」- 基础增删改查
泰克Tektronix示波器软件TDS420|TDS430|TDS460上位机软件NS-Scope
【MySQL必知必会】 范式 | ER模型
把@Transactional事务注解用到如此炉火纯青,真的强!
How to realize the frame selection of objects in canvas (6)
什么专业越老越吃香?
CMake Tutorial Tour (1)_Basic starting point
1.2Recyclerview实现Item点击事件
泰克Tektronix示波器软件TDS210|TDS220|TDS224上位机软件NS-Scope
随机推荐
这是一道非常有争议的题,我的分析如下: TCP/IP在多个层引入了安全机制,其中TLS协议位于______。 A.数据链路层 B.网络层 C.传输层 D.应用层
【微服务~Nacos】Nacos服务提供者和服务消费者
sqlserver 多行合并成一行
nacos集群配置详解
7.28
How to increase account weight?3 ways to operate your own media to help you get more revenue
微信小程序开发之图片压缩方案
软考 --- 数据库(5)数据库控制
npm ERR! code ENOTSUP npm ERR! notsup Unsupported engine for [email protected]: wanted: {“n
日期时间存入数据库会差一天?
STM32 - OLED display experiment
泰克Tektronix示波器软件TDS1012|TDS2002|TDS2004上位机软件NS-Scope
自学HarmonyOS应用开发(53)- 获取当前位置
Meetings OA To Be Meeting && All Meetings
@RequestParam注解的详细介绍
Running a Fabric Application
Fabric Private Data Case
Recommendation system: collection of user "behavioral data" [use Kafka and Cassandra to process data] [if it overlaps with business data, it also needs to be collected independently]
【微服务~Nacos】Nacos之配置中心
性能测试理论1 | 性能测试难点问题梳理