Skip to content

Commit 36bb740

Browse files
committed
Automatic merge from master.
1 parent 931e1d9 commit 36bb740

File tree

1 file changed

+21
-30
lines changed

1 file changed

+21
-30
lines changed

index.html

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,12 @@ <h1>JavaScript Garden</h1>
162162
differences between the two models. </p>
163163
<p>The first major difference is that inheritance in JavaScript is done by using so
164164
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>
165171
<pre><code>function Foo() {
166172
this.value = 42;
167173
}
@@ -171,16 +177,23 @@ <h1>JavaScript Garden</h1>
171177

172178
function Bar() {}
173179

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;
176186

177187
var test = new Bar() // create a new bar instance
178188

179189
// 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. */};
184197
</code></pre>
185198
<p>In the above, the object <code>test</code> will inherit from both <code>Bar.prototype</code> and
186199
<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>
206219
<pre><code>function Foo() {}
207220
Foo.prototype = 1; // no effect
208221
</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>
233224
</section><section><header><h3>Performance</h3></header>
234225
<p>The lookup time for properties that are high up on the prototype chain can have a
235226
negative impact on performance critical sections of code. Additionally, trying to

0 commit comments

Comments
 (0)