@@ -162,6 +162,12 @@ <h1>JavaScript Garden</h1>
162
162
differences between the two models. </ p >
163
163
< p > The first major difference is that inheritance in JavaScript is done by using so
164
164
called < em > prototype chains</ em > .</ p >
165
+ < aside >
166
+ < p > < strong > Note:</ strong > Simply using < code > Bar.prototype = Foo.prototype</ code > will result in both objects
167
+ sharing the < strong > same</ strong > prototype. Therefore, changes to either object its prototype
168
+ will affect the other its prototype as well, which in most cases is not the
169
+ desired effect.</ p >
170
+ </ aside >
165
171
< pre > < code > function Foo() {
166
172
this.value = 42;
167
173
}
@@ -171,16 +177,23 @@ <h1>JavaScript Garden</h1>
171
177
172
178
function Bar() {}
173
179
174
- // Set Bar's prototype to the prototype object of Foo
175
- Bar.prototype = Foo.prototype;
180
+ // Set Bar's prototype to a new instance of Foo
181
+ Bar.prototype = new Foo();
182
+ Bar.prototype.foo = 'Hello World';
183
+
184
+ // Make sure to list Bar as the actual constructor
185
+ Bar.prototype.constructor = Bar;
176
186
177
187
var test = new Bar() // create a new bar instance
178
188
179
189
// The resulting prototype chain
180
- Object.prototype: {toString: ... /* etc. */};
181
- Foo.prototype: {method: ...};
182
- Bar.prototype: Foo.prototype
183
- Bar.method()
190
+ test [instance of Bar]
191
+ Bar.prototype [instance of Foo]
192
+ { foo: 'Hello World' }
193
+ Foo.prototype
194
+ {method: ...};
195
+ Object.prototype
196
+ {toString: ... /* etc. */};
184
197
</ code > </ pre >
185
198
< p > In the above, the object < code > test</ code > will inherit from both < code > Bar.prototype</ code > and
186
199
< code > Foo.prototype</ code > ; hence, it will have access to the function < code > method</ code > that was
@@ -206,30 +219,8 @@ <h1>JavaScript Garden</h1>
206
219
< pre > < code > function Foo() {}
207
220
Foo.prototype = 1; // no effect
208
221
</ code > </ pre >
209
- < p > Assigning objects on the other hand will work, and allows for dynamic creation of
210
- prototype chains.</ p >
211
- < pre > < code > function Foo() {
212
- this.value = 42;
213
- }
214
- Foo.prototype = {
215
- method: function() {}
216
- };
217
-
218
- function Bar() {}
219
-
220
- Bar.prototype = new Foo();
221
- var boo = new Bar();
222
-
223
- // Resulting prototype chain
224
- Object.prototype: {toString: ... /* etc. */};
225
- Foo.prototype: {method: ...};
226
- [Foo Instance]: {value: 42};
227
- Bar.prototype: [Foo Instance]
228
- Bar.method()
229
- </ code > </ pre >
230
- < p > Now < code > Bar.prototype</ code > points to an < em > instance</ em > of < code > Foo</ code > ; thus, the property
231
- < code > value</ code > of that very instance is now on the prototype chain. And since < code > Foo</ code >
232
- itself has a prototype, the chain goes on with that one afterwards.</ p >
222
+ < p > Assigning objects, as shown in the example above, will work, and allows for dynamic
223
+ creation of prototype chains.</ p >
233
224
</ section > < section > < header > < h3 > Performance</ h3 > </ header >
234
225
< p > The lookup time for properties that are high up on the prototype chain can have a
235
226
negative impact on performance critical sections of code. Additionally, trying to
0 commit comments