当前位置:网站首页>object. Defineproperty basic usage
object. Defineproperty basic usage
2022-06-12 12:05:00 【Mustang (Mustang)】
object.defineProperty Basic use
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
/*
Define a new property for an object or modify an existing property of an object , And return this object
grammar :
Object.defineProperty( Parameters 1, Parameters 2, Parameters 3)
Parameters 1: Target audience
Parameters 2: The name of the attribute to be modified or added
Parameters 3: Some characteristics of target object properties ( It's an object )
Parameters 1:
value: Property value
Parameters 2:
writable: Whether object property values can be modified true allow false Don't allow
Parameters 3:
configurable: Whether object properties can be deleted true allow false Don't allow
Parameters 4:
enumerable: Whether object properties can be enumerated
Parameters 5:
get(): Give a property getter Method , This method is triggered when the property value of this object is accessed
Parameters 6:
set(): Give a property setter Method , This method is triggered when the property value is set
*/
//2、value
// var obj = {};
// Object.defineProperty(obj, "name", {
// value: " Zhang San "
// })
// Object.defineProperty(obj, "age", {
// value: "28"
// })
// console.log(obj)
//3、writable
// var obj = {};
// Object.defineProperty(obj, "name", {
// value: " Zhang San ",
// writable: false// When set to false The property value of the current object cannot be modified
// })
// obj.name = " Li Si "
// console.log(obj.name)// Zhang San
// var obj = {};
// Object.defineProperty(obj, "name", {
// value: " Zhang San ",
// writable: true// When set to true The property value of the current object can be modified
// })
// obj.name = " Li Si "
// console.log(obj.name)// Li Si
// 4、configurable
// var obj = {};
// Object.defineProperty(obj, "name", {
// value: " Zhang San ",
// configurable: false// When set to false The properties of the object are not allowed to be deleted
// })
// delete obj.name;
// console.log(obj.name)// Zhang San
// var obj = {};
// Object.defineProperty(obj, "name", {
// value: " Zhang San ",
// configurable: true// When set to true The properties of the object can be deleted
// })
// delete obj.name;
// console.log(obj.name)//undefined
// 5、enumerable
// var obj = { name: " Zhang San ", age: " Li Si " }
// Object.defineProperty(obj, "name", {
// enumerable: false// When set to false The properties of an object cannot be enumerated
// })
// Object.defineProperty(obj, "age", {
// enumerable: false
// })
// console.log(Object.keys(obj))//[]
// var obj = { name: " Zhang San ", age: " Li Si " }
// Object.defineProperty(obj, "name", {
// enumerable: true// When set to true The properties of an object can be enumerated
// })
// Object.defineProperty(obj, "age", {
// enumerable: true
// })
// console.log(Object.keys(obj))//["name",age]
//6、for in And Object.keys() The difference between
// function Person(name, age) {
// this.name = name;
// this.age = age;
// }
// Person.prototype = {
// sex: " male "
// }
// var man = new Person(" Zhang San ", 18);
// console.log(Object.keys(man));//["name","age"]
// for (var key in man) {
// console.log(key);//name age sex
// }
// summary :
// Object.keys(): Returns an array , The array value is the object's own attribute , Does not include inherited prototype properties
//
// for in : Traversing object enumerable properties , Include own attributes , And properties inherited from the prototype
// 7、get() && set()
var obj = { name: " Zhang San " }
Object.defineProperty(obj, "name", {
get() {
console.log(" Was interviewed ")// Triggered when accessed get() Method
},
set(newVal) {
console.log(" Has been set up " + newVal)// Triggered when set set() Method
}
})
obj.name// Output : Was interviewed
obj.name = " Li Si ";// Output : Li Si was set up
// Be careful : When using the get() Methods or set() Method cannot be used value and writable Otherwise, an error will be reported
</script>
</body>
</html>
边栏推荐
- Lambda and filter, index of list and numpy array, as well as various distance metrics, concatenated array and distinction between axis=0 and axis=1
- Data processing instructions of arm instruction set
- Reasons for SSL introduction and encryption steps
- TinyMCE realizes automatic uploading of pasted pictures
- ARM指令集之Load/Store访存指令(二)
- 影响店铺权重的三十一条,快来看看中招了吗
- mysql复习
- Cookies and sessions
- Ros- resolve error "tf2\u buffer\was not declared in this scope"
- Analyze the implementation principle of the onion model, and connect the onion model in your own project
猜你喜欢

Find the median of two ordered arrays (leetcode 4)

左右案例+小圆点的轮播图+无时间

PDSCH 相关

寻找两个有序数组的中位数(LeetCode 4)

TinyMCE series (I) TinyMCE environment construction

QML学习 第一天

QT adds a summary of the problems encountered in the QObject class (you want to use signals and slots) and solves them in person. Error: undefined reference to `vtable for xxxxx (your class name)‘

Dom+js+ carousel map + no time
![[foundation of deep learning] learning of neural network (4)](/img/8d/0e1b5d449afa583a52857b9ec7af40.png)
[foundation of deep learning] learning of neural network (4)

A.前缀极差
随机推荐
【Leetcode】79. Word search
作物模型的区域模拟实现过程初探
必杀技--使用FFmpeg命令快速精准剪切视频
导航中,添加边框影响布局的解决方法
The second regular match is inconsistent with the first one, and the match in the regular loop is invalid
Relation entre les classes et à l'intérieur des classes de classification vidéo - - Régularisation
Left and right cases + rotating pictures of small dots + no time
Google Earth engine (GEE) - quick land classification by kmeans clustering (double for loop quick parameter adjustment)
Dom+js+ carousel map + no time
Blue Bridge Cup 2015 CA provincial competition (filling the pit)
创建Servlet项目
视频分类的类间和类内关系——正则化
In navigation, the solution of adding borders to affect the layout
邻居子系统之邻居项状态更新
The first thing with a server
object.defineProperty 基本使用
this的指向
LeetCode 890. Find and replace mode (analog + double hash table)
ARM指令集之数据处理类指令
5G NR協議學習--TS38.211下行通道