-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathooJs2.js
96 lines (72 loc) · 2.08 KB
/
ooJs2.js
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
/* ************************************************************************************************
* 前端代码在线测试工具 https://jsbin.com/paximakoso/edit?js,output
*
* JavaScript面向对象编程
*
* ************************************************************************************************ */
/**
* 原型化继承与类式继承
*/
(function () {
/**
* 公共方法 原型上添加
* @param name
* @param func
* @returns {Function}
*/
Function.prototype.method = function (name, func) {
this.prototype[name] = func;
return this;
};
Function.method('inherits', function (parent) {
var depth = 0;
var proto = this.prototype=new parent();
this.method('uber', function uber(name) {
var func;
var ret;
var v = parent.prototype;
if (depth) {
for(var i = d; i > 0; i++){
v = v.constructor.prototype;
}
func = v[name];
} else {
func = proto[name];
if (func==this[name]) {
func = v[name];
}
}
depth += 1;
ret = func.apply(this, Array.prototype.slice.apply(arguments, [1]));
depth -= 1;
return ret;
});
return this;
});
Function.method('swiss', function (parent) {
for(var i = 1; i < arguments.length; i++){
var name = arguments[i];
this.prototype[name] = parent.prototype[name];
}
return this;
});
})(window);
(function () {
function Person(name) {
this.name = name;
}
Person.method("getname", function () {
return name;
});
function User(name, password) {
this.name = name;
this.password = password;
}
User.inherits(Person);
User.method("getpassword", function () {
return this.password;
});
User.method("getname", function () {
return "My name is " + this.uber('getname');
});
})();