-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
142 lines (102 loc) · 3.09 KB
/
index.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>class.js</title>
</head>
<body>
<script src="class.js"></script>
<script>
if(!("console" in window) || console == null){
var console = {};
console.log = function(){
alert([].slice.call(arguments).join(","))
};
}
// 配置项
// $Class.config({
// autoSuperConstructor:true, //当子类被实例化时是否先执行父类构造函数
// notUseNew:true, //是否可以不使用new直接调用方法实例化对像 如:A()
// useExtend:true, //是否使用让类拥有拓展继承的方法 如:B = A.$extend({})
// useSuper:true, //是否让类有$super属性访问父类成员 如:B.$super.foo()
// useConstructor:true //是否使用B.$constructor来保存定义的__构造函数,这里create inherit生成的构造函数是不等于__的
// });
/////////// 例一 /////////////
var Man = $Class.create({
//构造方法
__:function(m){
this.name = m||"uname";
//私有属性
var money = 100;
//私有方法
function buy(){
}
this.getmoney = function(){
return money;
}
},
//共有属性
sex:"男",
//共有方法
showname:function(){
alert(this.name);
}
});
var m = new Man("abc");
var m2 = Man('cde'); //new 可略
console.log(m, m2);
var ChunGe = $Class.inherit(Man,{
__:function(m){
this.type = m||"chunGe";
//私有属性
var money = 500,baqi = 10;
this.getmoney = function(){
alert(money);
};
this.baqi = function(){
alert(baqi);
}
},
alse:function(){
alert(this.sex);
}
});
var c = new ChunGe("chunchun");
var c2 = ChunGe("cc"); //new 可略
//检测c是否为chunGe的实例
console.log(c instanceof ChunGe);
//检测c是否为man的实例
console.log(c instanceof Man);
//构造函数
console.log(c.constructor);
//父类
console.log(c.constructor.$super);
/////////// 例二 /////////////
var A = $Class.create({
__:function(){
console.log("A");
}
});
//继承的第二种用法 直接通过 A.$extend
var B = A.$extend({
__:function(){
console.log("B");
}
}, true);//这里的true会导致实例化时A的构造也会被执行(相当于配置了autoSuperConstructor=true)
var b = B(); //可以不需要用new
var a = A();
console.log(b instanceof B, b instanceof A);
////////// 单例 //////////////
var S = $Class.singleton({name:"一个单例"});
var s1 = S(); //同样new可略
var s2 = new S();
console.log("单例类的两个实例是否相等",s1===s2);
var SubS = S.$extend({
sub:"子类继承单例类"
});
var s3 = SubS();
var s4 = SubS();
console.log("单例类可以继承,子类实例是否相等",s3===s4, s3, s4);
</script>
</body>
</html>