当前位置:网站首页>object.defineProperty 基本使用
object.defineProperty 基本使用
2022-06-12 11:58:00 【mustang(野马)】
object.defineProperty 基本使用
<!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>
/*
给一个对象定义一个新的属性或者在修改一个对象现有的属性,并返回这个对象
语法:
Object.defineProperty(参数1,参数2,参数3)
参数1:目标对象
参数2:要修改或者添加的属性名称
参数3:目标对象属性的一些特征 (是一个对象)
参数1:
value:属性值
参数2:
writable:对象属性值是否可以被修改 true允许 false不允许
参数3:
configurable:对象属性是否可以被删除 true允许 false不允许
参数4:
enumerable:对象属性是否可被枚举
参数5:
get():给一个属性提供getter方法,当访问这个对象的属性值得时候触发该方法
参数6:
set():给一个属性提供setter方法,当设置属性值得时候触发该方法
*/
//2、value
// var obj = {};
// Object.defineProperty(obj, "name", {
// value: "张三"
// })
// Object.defineProperty(obj, "age", {
// value: "28"
// })
// console.log(obj)
//3、writable
// var obj = {};
// Object.defineProperty(obj, "name", {
// value: "张三",
// writable: false//当设置为false的时候当前对象的属性值不允许被修改
// })
// obj.name = "李四"
// console.log(obj.name)//张三
// var obj = {};
// Object.defineProperty(obj, "name", {
// value: "张三",
// writable: true//当设置为true的时候当前对象的属性值允许被修改
// })
// obj.name = "李四"
// console.log(obj.name)//李四
// 4、configurable
// var obj = {};
// Object.defineProperty(obj, "name", {
// value: "张三",
// configurable: false//当设置为false的时候对象的属性不允许被删除
// })
// delete obj.name;
// console.log(obj.name)//张三
// var obj = {};
// Object.defineProperty(obj, "name", {
// value: "张三",
// configurable: true//当设置为true的时候对象的属性允许被删除
// })
// delete obj.name;
// console.log(obj.name)//undefined
// 5、enumerable
// var obj = { name: "张三", age: "李四" }
// Object.defineProperty(obj, "name", {
// enumerable: false//当设置为false的时候对象的属性不可被枚举
// })
// Object.defineProperty(obj, "age", {
// enumerable: false
// })
// console.log(Object.keys(obj))//[]
// var obj = { name: "张三", age: "李四" }
// Object.defineProperty(obj, "name", {
// enumerable: true//当设置为true的时候对象的属性可被枚举
// })
// Object.defineProperty(obj, "age", {
// enumerable: true
// })
// console.log(Object.keys(obj))//["name",age]
//6、for in 与Object.keys()的区别
// function Person(name, age) {
// this.name = name;
// this.age = age;
// }
// Person.prototype = {
// sex: "男"
// }
// var man = new Person("张三", 18);
// console.log(Object.keys(man));//["name","age"]
// for (var key in man) {
// console.log(key);//name age sex
// }
// 总结:
// Object.keys():返回一个数组,数组值为对象自有的属性,不会包括继承原型的属性
//
// for in :遍历对象可枚举属性,包括自身属性,以及继承自原型的属性
// 7、get() && set()
var obj = { name: "张三" }
Object.defineProperty(obj, "name", {
get() {
console.log("被访问了")//当被访问的时候会触发get()方法
},
set(newVal) {
console.log("被设置了" + newVal)//当被设置的时候会触发set()方法
}
})
obj.name//输出:被访问了
obj.name = "李四";//输出:被设置了李四
// 注意:当使用了get()方法或者set()方法的时候就不能使用value和writable中的任何一个属性否则会报错
</script>
</body>
</html>
边栏推荐
- TinyMCE series (III) introduction to common TinyMCE APIs
- Linear model of machine learning
- 6.6 Convolution de séparation
- Byte order (network / host) conversion
- TinyMCE realizes automatic uploading of pasted pictures
- 第六章 数据类型(五)
- 无重复字符的最长字符串(LeetCode 3)
- Unit test case framework --unittest
- Miscellaneous instructions of arm instruction set
- 必杀技--使用FFmpeg命令快速精准剪切视频
猜你喜欢

IP地址管理

Lambda and filter, List 和 numpy array的索引,以及各种距离指标distance-metrics,拼接数组以及axis=0 and axis=1的区分

TinyMCE realizes automatic uploading of pasted pictures

QT添加QObject类(想使用信号和槽)遇到的问题汇总,亲测解决有效error: undefined reference to `vtable for xxxxxx(你的类名)‘

Batch load/store instructions of arm instruction set

6.6 Convolution de séparation

Reprint --win10 open the task manager to solve the blue screen problem

Basic principle of Doppler effect

M-arch (fanwai 10) gd32l233 evaluation -spi drive DS1302

UML series articles (30) architecture modeling -- product diagram
随机推荐
Relatively rare exception records in UI automation test
Design of tablewithpage
Blue Bridge Cup 2015 CA provincial competition (filling the pit)
一个人必须不停地写作,才能不被茫茫人海淹没。
QT based travel query and simulation system
Shardingjdbc-5.1.0 monthly horizontal table splitting + read-write separation, automatic table creation and node table refresh
QML学习 第二天
Load/store memory access instruction of arm instruction set (1)
【数据库】sqlite版本升级、降级
Basic concepts of machine learning
如何确定首页和搜索之间的关系呢?首页与搜索的关系
文件夹目录结构自动生成
Unit test case framework --unittest
Multiplication instruction of arm instruction set
ARM指令集之Load/Store指令寻址方式(一)
Jump instruction of arm instruction set
【深度学习基础】神经网络的学习(4)
Byte order - how to judge the big end and the small end
ARM指令集之乘法指令
Compiling Draco library on Windows platform