1
1
## The Prototype
2
2
3
- JavaScript does not feature a classical inheritance model; instead, it uses a
4
- * prototypal* one.
3
+ JavaScript does not feature a classical inheritance model; instead, it uses a
4
+ * prototypal* one.
5
5
6
- While this is often considered to be one of JavaScript's weaknesses, the
6
+ While this is often considered to be one of JavaScript's weaknesses, the
7
7
prototypal inheritance model is in fact more powerful than the classic model.
8
8
It is, for example, fairly trivial to build a classic model on top of a
9
9
prototypal model, while the other way around is a far more difficult task.
10
10
11
11
JavaScript is the only widely used language that features prototypal
12
12
inheritance, so it can take time to adjust to the differences between the two
13
- models.
13
+ models.
14
14
15
15
The first major difference is that inheritance in JavaScript uses * prototype
16
16
chains* .
17
17
18
18
> ** Note:** Simply using ` Bar.prototype = Foo.prototype ` will result in both objects
19
- > sharing the ** same** prototype. Therefore, changes to either object's prototype
20
- > will affect the prototype of the other as well, which in most cases is not the
19
+ > sharing the ** same** prototype. Therefore, changes to either object's prototype
20
+ > will affect the prototype of the other as well, which in most cases is not the
21
21
> desired effect.
22
22
23
23
function Foo() {
@@ -36,26 +36,26 @@ chains*.
36
36
// Make sure to list Bar as the actual constructor
37
37
Bar.prototype.constructor = Bar;
38
38
39
- var test = new Bar() // create a new bar instance
39
+ var test = new Bar(); // create a new bar instance
40
40
41
41
// The resulting prototype chain
42
42
test [instance of Bar]
43
- Bar.prototype [instance of Foo]
43
+ Bar.prototype [instance of Foo]
44
44
{ foo: 'Hello World' }
45
45
Foo.prototype
46
46
{ method: ... }
47
47
Object.prototype
48
48
{ toString: ... /* etc. */ }
49
49
50
50
In the code above, the object ` test ` will inherit from both ` Bar.prototype ` and
51
- ` Foo.prototype ` ; hence, it will have access to the function ` method ` that was
51
+ ` Foo.prototype ` ; hence, it will have access to the function ` method ` that was
52
52
defined on ` Foo ` . It will also have access to the property ` value ` of the
53
53
** one** ` Foo ` instance that is its prototype. It is important to note that `new
54
- Bar()` does **not** create a new ` Foo` instance, but reuses the one assigned to
54
+ Bar()` does **not** create a new ` Foo` instance, but reuses the one assigned to
55
55
its prototype; thus, all ` Bar ` instances will share the ** same** ` value ` property.
56
56
57
- > ** Note:** Do ** not** use ` Bar.prototype = Foo ` , since it will not point to
58
- > the prototype of ` Foo ` but rather to the function object ` Foo ` . So the
57
+ > ** Note:** Do ** not** use ` Bar.prototype = Foo ` , since it will not point to
58
+ > the prototype of ` Foo ` but rather to the function object ` Foo ` . So the
59
59
> prototype chain will go over ` Function.prototype ` and not ` Foo.prototype ` ;
60
60
> therefore, ` method ` will not be on the prototype chain.
61
61
@@ -71,7 +71,7 @@ hasn't found the specified property, it will return the value
71
71
### The Prototype Property
72
72
73
73
While the prototype property is used by the language to build the prototype
74
- chains, it is still possible to assign ** any** given value to it. However,
74
+ chains, it is still possible to assign ** any** given value to it. However,
75
75
primitives will simply get ignored when assigned as a prototype.
76
76
77
77
function Foo() {}
@@ -85,22 +85,22 @@ creation of prototype chains.
85
85
The lookup time for properties that are high up on the prototype chain can have
86
86
a negative impact on performance, and this may be significant in code where
87
87
performance is critical. Additionally, trying to access non-existent properties
88
- will always traverse the full prototype chain.
88
+ will always traverse the full prototype chain.
89
89
90
- Also, when [ iterating] ( #object.forinloop ) over the properties of an object
90
+ Also, when [ iterating] ( #object.forinloop ) over the properties of an object
91
91
** every** property that is on the prototype chain will be enumerated.
92
92
93
93
### Extension of Native Prototypes
94
94
95
95
One mis-feature that is often used is to extend ` Object.prototype ` or one of the
96
96
other built in prototypes.
97
97
98
- This technique is called [ monkey patching] [ 1 ] and breaks * encapsulation* . While
99
- used by popular frameworks such as [ Prototype] [ 2 ] , there is still no good
98
+ This technique is called [ monkey patching] [ 1 ] and breaks * encapsulation* . While
99
+ used by popular frameworks such as [ Prototype] [ 2 ] , there is still no good
100
100
reason for cluttering built-in types with additional * non-standard* functionality.
101
101
102
- The ** only** good reason for extending a built-in prototype is to backport
103
- the features of newer JavaScript engines; for example,
102
+ The ** only** good reason for extending a built-in prototype is to backport
103
+ the features of newer JavaScript engines; for example,
104
104
[ ` Array.forEach ` ] [ 3 ] .
105
105
106
106
### In Conclusion
0 commit comments