当前位置:网站首页>"Object.defineproperty" [mandatory for interview business]
"Object.defineproperty" [mandatory for interview business]
2022-07-21 04:48:00 【A good programmer】
Grammar definition : There are two main forms of property descriptors in objects : Data descriptor and Access descriptor .
Object.defineProperty()
Method defines a new property directly on an object , Or modify an object's existing
attribute , And return this object .
grammar
//obj: The object to define the property
//prop: The name of the property to be defined or modified
//descriptor:{}
Object.defineProperty(obj, prop, descriptor)
The first part : Data descriptor It's a property with value , The value can be writable , It can also be unwritten .
// Three default characteristics of data descriptor :
1. Do not modify the :
2. Non rewritable :
3. countless :
let obj = {}
Object.defineProperty(obj,'a',{
value:1,
})
console.log(obj) //{a:1}
Key values that data descriptors can have :
configurable: If and only if the configurable
The key value is true
when , This attribute can also be
Delete . The default is false
/*
If and only if the configurable The key value is true when ,
This attribute can be deleted from the corresponding object . The default is false.
*/
let obj = {}
Object.defineProperty(obj,'a',{
value:1,
configurable:true,
})
// configurable:true Deleting
delete obj.a
console.log(obj) //{} Print an empty string
enumerable: If and only if the enumerable
The key value is true
when , This property will appear in the enumeration property of the object . The default is false
.
/*
If and only if the enumerable The key value is true when , This attribute will appear in the object
In the enumeration properties of . The default is false.
*/
let obj = {}
Object.defineProperty(obj,'a',{
value:1,
enumerable:true,
})
for (const key in obj) {
console.log(key)//enumerable:false countless : Can't print out
}
value: The value corresponding to this property . It can be anything that works JavaScript value ( The number , object , Functions, etc ). The default is
The given property name is undefined : Default undefined
writable: If and only if the writable
The key value is true
when , The value of the property , That's the top value
, Can be changed by the assignment operator . The default is false
.
/*
If and only if the writable The key value is true when , The value of the property , That's up there
Of value, Can be assigned to the operator (en-US) change . The default is false.
*/
let obj = {}
Object.defineProperty(obj,'a',{
value:1,
writable:true
})
obj.a = 2
console.log(obj) //{a:2}
//writable:false It can't be changed a Value
The second part : Access descriptor : Access descriptors are created by getter Functions and setter The properties described by the function .
/*
getter
Attribute getter function , without getter, Then for undefined.
When accessing this property , This function will be called . No parameters are passed in during execution , But it will pass in this object
The return value of this function will be used as the value of the property .
*/
/*
set
Attribute setter function , without setter, Then for undefined.
When the property value is modified , This function will be called . This method takes a parameter ( That is, the new value given ),
The this object .
*/
let obj = {}
Object.defineProperty(obj,'a',{
get(){
// visit a Time call
console.log('get a:',1) // to get Inside a assignment 1
return 1
},
set(newValue){
// modify a Start when the value of : Note that there is no return
// newValue: For the modified attribute value
console.log('set a:',newValue)
}
})
The third part :Object.defineProperties
<script>
let obj = {}
let num = 1
Object.defineProperties(obj,{
a:{
get(){
return num++
},
set(){
}
},
b:{
value:1
}
})
if(obj.a===1&&obj.a===2&&obj.a===3&&obj.a===4){
console.log(' Hello ')
}
console.log(obj)
</script>
边栏推荐
- FigDraw 12. SCI 文章绘图之相关性矩阵图(Correlation Matrix)
- JS基础--Object静态方法
- Super dry goods: design summary, tools and technical points of data visualization are all available
- HCIA knowledge summary
- If:4+ iron metabolism and immune related gene markers predict clinical outcomes and molecular characteristics of triple negative breast cancer
- c语言文件操作管理(上)
- 文末送书|豆瓣9.4分,“hello,world”起源于这本书!
- SCS【1】今天开启单细胞之旅,述说单细胞测序的前世今生
- Back to the origin: start learning again
- Solve tensoflow2 No module named:tensorflow contrib
猜你喜欢
如何合并多个工作表或多个工作簿?3种合并方法都在这
Super dry goods: design summary, tools and technical points of data visualization are all available
FigDraw 14. SCI 文章绘图之和弦图及文章复现(Chord Diagram)
文末送书|豆瓣9.4分,“hello,world”起源于这本书!
如何培养真正的数据分析思维?附实践案例
RNA 24. SCI文章中基于TCGA的免疫浸润细胞分析的在线小工具——TIMER
DOM操作--获取元素和节点
FigDraw 12. SCI 文章绘图之相关性矩阵图(Correlation Matrix)
DNA 9. 揭秘肿瘤异质性与TMB, MSI之间的相关性
RNA 20. SCI 文章中单样本免疫浸润分析 (ssGSEA)
随机推荐
游戏实现——扫雷
RNA 23. SCI文章中表达基因Cox模型的风险因子关联图(ggrisk)
Directory and file management
掌握这些插件,分分钟提高你的办公效率90%!
JS基础--Object静态方法
JS笔试题--原型,new,this综合题
【leetcode】150 逆波兰表达式求值
[latex] miktex+texstudio installation and configuration of thesis writing environment
在 IDEA 里下个五子棋不过分吧?
「Object.defineProperty」【面试业务必备】
JS面试题--ES5和ES6有什么区别?
动态调试JS代码
Database notes
Three methods: arrange strings in reverse order (instead of printing in reverse order)
The latest upx3.91 supports win64 / PE plus / minus shell
c语言文件操作管理(上)
学会如何选择图表类型,小白也能玩转数据分析
QQ三方登录 - 前置环境和交互
TCP和UDP的区别
Concepts de base de la langue C - petites connaissances une fois par jour