Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6a3ce88

Browse files
more classical inheritance pattern
1 parent 7deee56 commit 6a3ce88

File tree

1 file changed

+58
-2
lines changed

1 file changed

+58
-2
lines changed

‎06-code-reuse.md‎

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Prefer composition over inheritance.
77
* play on the word 'class', nothing to do with the word classical
88
* JavaScript has no classes but constructor functions make same people think that
99
* use the term constructor function
10+
* probably a bad idea but worth knowing about
1011

1112
```js
1213
function Parent(){ }
@@ -17,7 +18,7 @@ inherit(Child, Parent);
1718

1819
* inherit is not part of the language have to implement it yourself
1920

20-
## classical inheritance with default pattern
21+
## classical default pattern
2122

2223
```js
2324
function inherit(Child, Parent){
@@ -39,7 +40,7 @@ function inherit(Child, Parent){
3940
* properties are looked up by walking through this prototype chain:
4041
* if an object doesn't have a property it's ```__proto__``` is consulted
4142

42-
## classical inheritance with rent-a-constructor pattern
43+
## classical rent-a-constructor pattern
4344

4445
```js
4546
function Child(a, b, c, d){
@@ -57,3 +58,58 @@ function Child(a, b, c, d){
5758
* no proto links are kept, can't access parent prototype properties
5859
* multiple inheritance can be achieved by applying more than one constructors
5960
* 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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /