-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChapter6_OO_1.html
107 lines (95 loc) · 2.54 KB
/
Chapter6_OO_1.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>面向对象设计</title>
</head>
<body>
<script type="text/javascript">
//////////////数据属性////////////////////////
var person=new Object();
person.name="John";
person.age=19;
person.sayname=function ( s) {
console.log(s);
}
var ps2={};ps2.name="Niko";ps2.age=18;
var ps3={name:"Niko",age:18};
//定义属性不可更改
Object.defineProperty(person,"name",{
writable:false
})
person.name="NEW JOHN"
console.log(person.name)
//不可删除属性
Object.defineProperty(person,"name",{
configurable:false,
value:"Nicholas"
})
console.log(person.name)
// 一旦把属性定义为不可配置的,
// 就不能再把它变回可配置了。
// delete person.name
// Object.defineProperty(person,"name",{
// configurable:true,//error
// value:"Nicholas"
// })
//所有方法
Object.defineProperty(ps2,"age",{
configurable:false,
enumerable:false,
writable:false,
value:28
})
//enumerable=false 所以只有一个属性输出
for (var i in ps2){
console.log(ps2[i])
}
///////访问器属性/////////////////////
var book={};
book._year=2004;
book.edition=1;
Object.defineProperty(book,"year",{
get:function () {
return this._year;
},
set:function (newValue) {
if(newValue>2004){
this._year=newValue;
this.edition+=1;
}
}
});
book.year=2005;
console.log(book._year)
console.log(book.edition)
/////Object.defineProperties()/////////////////
book={};
Object.defineProperties(book,{
_year:{
value:2004
},
edition:{
value:1
},
year:{
get:function () {
return this._year
},
set:function (v) {
this._year=v;
}
}
})
////////////////Object.getOwnPropertyDescriptor()///////////////////////
var op = Object.getOwnPropertyDescriptor(book,"_year");
console.log(op.configurable)
console.log(op.value)
console.log(typeof op.get)
var op2 = Object.getOwnPropertyDescriptor(book, "year");
console.log(op2.get)
console.log(op2.value)
console.log(typeof op2.get)
</script>
</body>
</html>