@@ -4,16 +4,16 @@ JavaScript does not feature a classical inheritance model; instead, it uses a
4
4
* prototypal* one.
5
5
6
6
While this is often considered to be one of JavaScript's weaknesses, the
7
- prototypal inheritance model is in fact more powerful than the classic model.
8
- It is, for example, fairly trivial to build a classic model on top of it, while the
9
- other way around is a far more difficult task.
7
+ prototypal inheritance model is in fact more powerful than the classic model.
8
+ It is, for example, fairly trivial to build a classic model on top of a
9
+ prototypal model, while the other way around is a far more difficult task.
10
10
11
- Due to the fact that JavaScript is basically the only widely used language that
12
- features prototypal inheritance, it takes some time to adjust to the
13
- differences between the two models.
11
+ JavaScript is the only widely used language that features prototypal
12
+ inheritance, so it can take time to adjust to the differences between the two
13
+ models.
14
14
15
- The first major difference is that inheritance in JavaScript is done by using so
16
- called * prototype chains* .
15
+ The first major difference is that inheritance in JavaScript uses * prototype
16
+ chains* .
17
17
18
18
> ** Note:** Simply using ` Bar.prototype = Foo.prototype ` will result in both objects
19
19
> sharing the ** same** prototype. Therefore, changes to either object's prototype
@@ -47,7 +47,7 @@ called *prototype chains*.
47
47
Object.prototype
48
48
{ toString: ... /* etc. */ }
49
49
50
- In the above, the object ` test ` will inherit from both ` Bar.prototype ` and
50
+ In the code above, the object ` test ` will inherit from both ` Bar.prototype ` and
51
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
@@ -64,7 +64,7 @@ its prototype; thus, all `Bar` instances will share the **same** `value` propert
64
64
When accessing the properties of an object, JavaScript will traverse the
65
65
prototype chain ** upwards** until it finds a property with the requested name.
66
66
67
- When it reaches the top of the chain - namely ` Object.prototype ` - and still
67
+ If it reaches the top of the chain - namely ` Object.prototype ` - and still
68
68
hasn't found the specified property, it will return the value
69
69
[ undefined] ( #core.undefined ) instead.
70
70
@@ -82,20 +82,21 @@ creation of prototype chains.
82
82
83
83
### Performance
84
84
85
- The lookup time for properties that are high up on the prototype chain can have a
86
- negative impact on performance critical sections of code. Additionally, trying to
87
- access non-existent properties will always traverse the full prototype chain.
85
+ The lookup time for properties that are high up on the prototype chain can have
86
+ a negative impact on performance, and this may be significant in code where
87
+ performance is critical. Additionally, trying to access non-existent properties
88
+ will always traverse the full prototype chain.
88
89
89
90
Also, when [ iterating] ( #object.forinloop ) over the properties of an object
90
- ** every** property that is on the prototype chain will get enumerated.
91
+ ** every** property that is on the prototype chain will be enumerated.
91
92
92
93
### Extension of Native Prototypes
93
94
94
95
One mis-feature that is often used is to extend ` Object.prototype ` or one of the
95
96
other built in prototypes.
96
97
97
98
This technique is called [ monkey patching] [ 1 ] and breaks * encapsulation* . While
98
- used by widely spread frameworks such as [ Prototype] [ 2 ] , there is still no good
99
+ used by popular frameworks such as [ Prototype] [ 2 ] , there is still no good
99
100
reason for cluttering built-in types with additional * non-standard* functionality.
100
101
101
102
The ** only** good reason for extending a built-in prototype is to backport
@@ -104,11 +105,12 @@ the features of newer JavaScript engines; for example,
104
105
105
106
### In Conclusion
106
107
107
- It is a ** must** to understand the prototypal inheritance model completely
108
- before writing complex code which makes use of it. Also, watch the length of
109
- the prototype chains and break them up if necessary to avoid possible
110
- performance issues. Further, the native prototypes should ** never** be extended
111
- unless it is for the sake of compatibility with newer JavaScript features.
108
+ It is ** essential** to understand the prototypal inheritance model before
109
+ writing complex code that makes use of it. Also, be aware of the length of the
110
+ prototype chains in your code and break them up if necessary to avoid possible
111
+ performance problems. Further, the native prototypes should ** never** be
112
+ extended unless it is for the sake of compatibility with newer JavaScript
113
+ features.
112
114
113
115
[ 1 ] : http://en.wikipedia.org/wiki/Monkey_patch
114
116
[ 2 ] : http://prototypejs.org/
0 commit comments