@@ -7,6 +7,7 @@ Prefer composition over inheritance.
7
7
* play on the word 'class', nothing to do with the word classical
8
8
* JavaScript has no classes but constructor functions make same people think that
9
9
* use the term constructor function
10
+ * probably a bad idea but worth knowing about
10
11
11
12
``` js
12
13
function Parent (){ }
@@ -17,7 +18,7 @@ inherit(Child, Parent);
17
18
18
19
* inherit is not part of the language have to implement it yourself
19
20
20
- ## classical inheritance with default pattern
21
+ ## classical default pattern
21
22
22
23
``` js
23
24
function inherit (Child , Parent ){
@@ -39,7 +40,7 @@ function inherit(Child, Parent){
39
40
* properties are looked up by walking through this prototype chain:
40
41
* if an object doesn't have a property it's ``` __proto__ ``` is consulted
41
42
42
- ## classical inheritance with rent-a-constructor pattern
43
+ ## classical rent-a-constructor pattern
43
44
44
45
``` js
45
46
function Child (a , b , c , d ){
@@ -57,3 +58,58 @@ function Child(a, b, c, d){
57
58
* no proto links are kept, can't access parent prototype properties
58
59
* multiple inheritance can be achieved by applying more than one constructors
59
60
* in case of multiple inheritance and duplicate properties - last one wins
61
+
62
+ ## classical rent-and-set prototype
63
+
64
+ ``` js
65
+ function Child (a , b , c , d ){
66
+ Parent .apply (this , arguments );
67
+ }
68
+ Child .prototype = new Parent ();
69
+ ```
70
+ * combine the two patterns above
71
+ * children gets copies of parents own members
72
+ * and references re-usable functionality (from parents prototype)
73
+ * drawback: parent constructor is called twice
74
+
75
+ ## classical share the prototype pattern
76
+
77
+ ``` js
78
+ function inherit (Child , Parent ){
79
+ C .prototype = P .prototype ;
80
+ }
81
+ ```
82
+ * no calls to the parent construtor at all
83
+ * just share prototype as reusable functionality should be in the prototype anyways
84
+ * fast lookups as all object references one prototype
85
+ * BUT children can modify parent behaviour
86
+
87
+ ## classical temporary constructor pattern
88
+
89
+ ``` js
90
+ function inherit (Child ,Parent ){
91
+ var Temp = function (){};
92
+ Temp .prototype = Parent .prototype ;
93
+ Child .prototype = new Temp ();
94
+ }
95
+ ```
96
+ * a.k.a. proxy constructor
97
+ * similar to default pattern but proxying via Temp constructor
98
+ * Temp constructor makes sure we only inherit prototype properties
99
+ * storing 'superclass' may be achieved by ``` Child.uber = Parent.prototype ```
100
+ * may also reset the constructor property on the prototype (``` Child.prototype.constructor=Child ``` )
101
+ * constructor property otherwise equals to 'Parent' and may be confusing when introspecting
102
+ * optimized version with immediate function and temp function in closure:
103
+
104
+ ``` js
105
+
106
+ var inherit = (function (){
107
+ var Temp = function (){};
108
+ return function (Child ,Parent ){
109
+ Temp .prototype = Parent .prototype ;
110
+ Child .prototype = new Temp ();
111
+ Child .uber = Parent .prototype ;
112
+ Child .prototype .constructor = Child;
113
+ }
114
+ })();
115
+ ```
0 commit comments