forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinheritance.js
More file actions
71 lines (68 loc) · 2.26 KB
/
inheritance.js
File metadata and controls
71 lines (68 loc) · 2.26 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
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
/**
* @private
* @deprecated
* Implementation of inheritance for JavaScript objects
* e.g. Class can access all of Base's function prototypes
* <pre lang="javascript"><code>
* Base = function () {}
* Class = function () {}
* Class = Class.extendsFrom(Base)
* </code></pre>
* @param {Object} Super
*/
Function.prototype.extendsFrom = function (Super) {
var Self, Func;
var Temp = function () {};
Self = this;
Func = function () {
Super.apply(this, arguments);
Self.apply(this, arguments);
this.constructor = Self;
};
Func._super = Super.prototype;
Temp.prototype = Super.prototype;
Func.prototype = new Temp();
return Func;
};
pc.extend(pc, function () {
return {
/**
* @private
* @function
* @name pc.inherits
* @description Implementation of inheritance for JavaScript objects
* e.g. Class can access all of Base's function prototypes
* The super classes prototype is available on the derived class as _super
* @param {Function} Self Constructor of derived class
* @param {Function} Super Constructor of base class
* @returns {Function} New instance of Self which inherits from Super
* @example
* Base = function () {};
* Base.prototype.fn = function () {
* console.log('base');
* };
* Class = function () {}
* Class = pc.inherits(Class, Base);
* Class.prototype.fn = function () {
* // Call overridden method
* Class._super.fn();
* console.log('class');
* };
*
* var c = new Class();
* c.fn(); // prints 'base' then 'class'
*/
inherits: function (Self, Super) {
var Temp = function () {};
var Func = function (arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) {
Super.call(this, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
Self.call(this, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
// this.constructor = Self;
};
Func._super = Super.prototype;
Temp.prototype = Super.prototype;
Func.prototype = new Temp();
return Func;
}
};
}());