-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsclass.html
193 lines (183 loc) · 4.78 KB
/
jsclass.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JS Class Extend and Event</title>
<script type="text/javascript">
// http://ejohn.org/blog/simple-javascript-inheritance/
var Class = (function(UNDEF) {
var initializing = false, regex = /xyz/.test(function() {
'xyz';
}) ? /\b_super\b/ : /.*/, fn = function() {
}, isFn = function(fn) {
return (typeof fn === 'function');
}, removeItem = function(array, item) {
for ( var i = 0, l = array.length; i < l; i++) {
if (array[i] === item) {
array.splice(i, 1);
i--;
}
}
}, getListener = function(obj, type, force) {
var allListeners = obj.__allListeners || force && (obj.__allListeners = {});
type = type.toLowerCase();
return allListeners && (allListeners[type] || force && (allListeners[type] = []));
}, extend = function(obj) {
var _super = this.prototype, proto, name, isOverwriting;
initializing = true;
proto = new this();
initializing = false;
for (name in obj) {
isOverwriting = isFn(obj[name]) && isFn(_super[name]) && regex.test(obj[name]);
proto[name] = isOverwriting ? (function(name, fn) {
return function() {
var tmp = this._super, ret;
this._super = _super[name];
ret = isFn(fn) ? fn.apply(this, arguments) : void (0);
this._super = tmp;
return ret;
};
})(name, obj[name]) : obj[name];
}
function Klass() {
if (!initializing && isFn(this.init)) {
this.init.apply(this, arguments);
}
}
Klass.prototype = proto;
Klass.constructor = Klass;
Klass.extend = arguments.callee;
return Klass;
}, Class = extend.call(fn, {
on : function(type, listener) {
getListener(this, type, true).push(listener);
},
un : function(type, listener) {
var listeners = getListener(this, type);
listeners && removeItem(listeners, listener);
},
fire : function(type) {
var listeners = getListener(this, type), r = null, t, k;
if (listeners) {
k = listeners.length;
while (k--) {
t = listeners[k].apply(this, arguments);
if (t !== UNDEF) {
r = t;
}
}
}
if (t = this['on' + type.toLowerCase()]) {
r = t.apply(this, arguments);
}
return r;
}
});
return Class;
})();
</script>
<script type="text/javascript">
var failCount = 0;
function startup() {
testExtend();
testEvent();
if (!failCount) {
alert('测试全部通过!');
}
}
function assert(desc, test) {
if (arguments.length === 1) {
test = desc;
}
if ('function' === typeof test) {
test = test() !== false;
}
if (!test) {
++failCount;
throw new Error("assertion failed: " + desc);
}
}
function testExtend() {
var Person = Class.extend({
init : function(isDancing) {
this.dancing = isDancing;
},
dance : function() {
return this.dancing;
}
});
var Nezha = Person.extend({
init : function() {
this._super(false);
},
dance : function() {
return this._super();
},
sing : function() {
return true;
}
});
var p = new Person(true);
assert('p的dancing应该为true', p.dance());
var n = new Nezha();
assert('n的dancing应该为false', !n.dance());
assert('n的sing()应该返回true', n.sing())
assert(p instanceof Person);
assert(p instanceof Class);
assert(n instanceof Nezha);
assert(n instanceof Person);
assert(n instanceof Class);
}
function testEvent() {
var Control = Class.extend({
init : function(container) {
this.container = container;
},
render : function() {
this.container.onclick = bind(this._proxyDomEvent, this);
this.fire('ready');
},
_proxyDomEvent : function(evt) {
evt = window.event || evt;
return this.fire(evt.type.replace(/^on/, ''), evt);
}
});
var elem = document.getElementById('test');
var control = new Control(elem);
var eventListener = function(type, evt) {
assert(type);
if (type === 'click') {
assert(evt);
}
alert('[' + type + '] event occurring!');
};
// 绑定监听
control.on('ready', eventListener); // 测试监听自定义事件
control.on('click', eventListener); // 测试监听DOM事件
// 触发事件
control.render();
invokeClick(elem); // 模拟DOM事件的触发
}
function bind(fn, context) {
return function() {
return fn.apply(context, arguments);
};
}
// 触发元素的点击事件
function invokeClick(element) {
if (element.click) { // IE 8,FireFox 12,Chrome 19
element.click();
} else if (element.fireEvent) {
element.fireEvent('onclick');
} else if (document.createEvent) { // Safari 5.1.2
var evt = document.createEvent('MouseEvents');
evt.initEvent('click', true, true);
element.dispatchEvent(evt);
}
}
</script>
</head>
<body onload="startup();">
<div id="test">Test pass in [IE 8, FireFox 12, Chrome 19, Safari 5].</div>
</body>
</html>