-
Notifications
You must be signed in to change notification settings - Fork 476
/
Copy path01.Object.defineProperty().html
60 lines (49 loc) · 1.75 KB
/
01.Object.defineProperty().html
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
60
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<link rel="stylesheet" href="">
<script src="vue.js"></script>
</head>
<body>
<script>
var o = {}; // 创建一个新对象
//configurable
//当且仅当该属性的 configurable 为 true 时,该属性才能够被改变,也能够被删除。默认为 false。
//enumerable
//当且仅当该属性的 enumerable 为 true 时,该属性才能够出现在对象的枚举属性中。默认为 false。
//value
//该属性对应的值。可以是任何有效的 JavaScript 值(数值,对象,函数等)。默认为 undefined。
//writable
//当且仅当仅当该属性的writable为 true 时,该属性才能被赋值运算符改变。默认为 false。
console.log('01 o:' + JSON.stringify(o));
// Example of an object property added with defineProperty with a data property descriptor
Object.defineProperty(o, "a", {
value: 37,
writable: true,
enumerable: true,
configurable: true
});
// 对象o拥有了属性a,值为37
console.log('02 o:' + JSON.stringify(o));
// Example of an object property added with defineProperty with an accessor property descriptor
var bValue;
Object.defineProperty(o, "b", {
get: function() {
return bValue;
},
set: function(newValue) {
bValue = newValue;
},
enumerable: true,
configurable: true
});
o.b = 38;
console.log('03 o:' + JSON.stringify(o));
// 对象o拥有了属性b,值为38
// The value of o.b is now always identical to bValue, unless o.b is redefined
</script>
</body>
</html>