-
Notifications
You must be signed in to change notification settings - Fork 2
/
6.20.html
85 lines (72 loc) · 2.48 KB
/
6.20.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="2.css" type="text/css">
</head>
<body>
<ul id="results"></ul>
<script type="text/javascript" src="2.js"></script>
<script type="text/javascript">
//子类方法
(function(){
var initializing = false,
superPattern =
/xyz/.test(function(){xyz;})?/\b_super\b/:/.*/;
Object.subClass = function(properties){
var _super = this.prototype;
initializing = true;
var proto = new this();
initializing = false;
for(var name in properties){
proto[name] = typeof properties[name] == "function" &&
typeof _super[name] == "function" &&
superPattern.test(properties[name])?
(function(name,fn){
return function(){
var tmp = this._super;
this._super = _super[name];
var ret = fn.apply(this,arguments);
this._super = tmp;
return ret;
};
})(name,properties[name]) :
properties[name];
}
function Class(){
if(!initializing && this.init){
this.init.apply(this,arguments);
}
}
Class.prototype = proto;
Class.constructor = Class;
Class.subClass = arguments.callee;
return Class;
};
})();
//经典继承语法示例
var Person = Object.subClass({
init:function(isDancing){
this.dancing = isDancing;
},
dance:function(){
return this.dancing;
}
});
var Ninja = Person.subClass({
init:function(){return this._super();},
swingSword:function(){
return true;
}
});
var person = new Person(true);
assert(person.dance(),"The sword is swinging.");
var ninja = new Ninja();
assert(ninja.swingSword(),"The sword is swinging.");
assert(!ninja.dance(),"The ninja is not dancing.");
assert(person instanceof Person,"Person is a Person.");
assert(ninja instanceof Ninja && ninja instanceof Person,"Ninja is a Ninja and a Person.");
</script>
</body>
</html>