-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefineProperty.js
More file actions
59 lines (45 loc) · 984 Bytes
/
Copy pathdefineProperty.js
File metadata and controls
59 lines (45 loc) · 984 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
let obj = {
a: 1,
b: 2,
c: 3,
d: 4,
e: [
1, 2, 3, 4
]
}
Object.defineProperty(obj, 'a', {
configurable: false
})
delete obj.a
console.log(obj) //a无法被删除
Object.defineProperty(obj, 'b', {
enumerable: false
})
for (let key in obj) {
console.log('key', key) //没有b
}
console.log(obj) // 直接打印obj也看不到b属性
console.log(obj.b) //但可以正常读取
Object.defineProperty(obj, 'c', {
writable: false
})
obj.c = 5
console.log(obj) //c无法被修改
Object.defineProperty(obj, 'd', { // 设置了get或set后属性将不包含数值
get() {
// console.log('this', this)
return this.e
},
set(value) {
this.e = value
}
})
obj.d = [3, 4, 5, 6, 7, 8, 9]
console.log(obj.d)
console.log(
Object.getOwnPropertyDescriptor(obj,'a'),
Object.getOwnPropertyDescriptor(obj,'b'),
Object.getOwnPropertyDescriptor(obj,'c'),
Object.getOwnPropertyDescriptor(obj,'d'),
Object.getOwnPropertyDescriptor(obj,'e'),
)