-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprototype.inheritsFrom.func.js
More file actions
46 lines (39 loc) · 1.61 KB
/
prototype.inheritsFrom.func.js
File metadata and controls
46 lines (39 loc) · 1.61 KB
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
// requires prototype.typeName.func.js
Function.prototype.inheritsFrom = function(parentClassOrObject) {
if (parentClassOrObject instanceof Object) {
if (typeof this.prototype.inherits == "undefined") {
this.prototype.inherits = [];
}
if (!(parentClassOrObject.typeName() in this.prototype.inherits)) {
// alert(this.typeName() + " Prototype inheritance from " + parentClassOrObject.typeName());
var inheritanceTarget = null;
if (parentClassOrObject instanceof Function) {
inheritanceTarget = parentClassOrObject.prototype;
} else {
inheritanceTarget = parentClassOrObject;
}
for (var parentPMemberIndex in inheritanceTarget) {
// avoid inheritance of certain elements from the parent
switch (parentPMemberIndex) {
case "inherits":
case "inheritsFrom":
case "typeName":
case "constructor":
continue;
default:
this.prototype[parentPMemberIndex] = inheritanceTarget[parentPMemberIndex];
}
}
this.prototype.inherits[parentClassOrObject.typeName()] = inheritanceTarget;
}
}
};
Object.prototype.inheritsFrom = function(parentClassOrObject) {
if (parentClassOrObject instanceof Object) {
this.constructor.inheritsFrom(parentClassOrObject);
// alert(this.typeName() + " Instance inheritance from " + parentClassOrObject.typeName());
if (parentClassOrObject instanceof Function) {
parentClassOrObject.apply(this, Array.prototype.slice.apply(arguments, [1]));
}
}
};