Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The line:</p> <pre><code>var bar = new Object( foo ); </code></pre> <p>In your first snippet, it doesn't do anything -you are right with your assumption-, it will simply return a reference to the same object passed to the <code>Object</code> constructor.</p> <p>That's the behavior when you pass a <em>native object</em> to the <code>Object</code> constructor in a <code>new</code> expression (<a href="http://es5.github.com/#x15.2.2.1" rel="nofollow noreferrer"><code>new Object(value)</code></a>), if you pass a <a href="http://webcache.googleusercontent.com/search?q=cache:7jGjZFWhwsgJ:www.jibbering.com/faq/#hostObject" rel="nofollow noreferrer"><em>host object</em></a>, the results are implementation dependent.</p> <p>If you don't pass a value (or you explicitly pass the primitives <code>undefined</code> or <code>null</code>) a new object that inherits from <code>Object.prototype</code> will be created.</p> <p>Otherwise, if you pass any of the remaining primitives (as a Number, String or a Boolean value), a <em>primitive wrapper object</em> will be created (basically "primitive-to-object" type conversion), for example.</p> <pre><code>var s = new String("foo"); // a string object wrapper typeof s; // "object" s.valueOf(); // "foo" </code></pre> <p><sup> See this question about primitives and objects: <a href="https://stackoverflow.com/questions/3907613/how-is-a-javascript-string-not-an-object">How is a Javascript string not an object?</a></sup></p> <p>In your second snippet, the line:</p> <pre><code>var bar = Object.create( foo ); </code></pre> <p>Creates a new object, that inherits from <code>foo</code>, and since it's a different object, when you assign the properties:</p> <pre><code>bar.three = 3; bar.one = 100; </code></pre> <p>Those will be created physically on that separated <em>instance</em>, as you can see, the <code>bar.one</code> property <strong>shadows</strong> the value contained in <code>foo</code>.</p> <p>The object referenced by <code>bar</code>, in fact will contain two <strong>own properties</strong> (<code>one</code> and <code>three</code>, but since it inherits from <code>foo</code>, the property named <code>two</code> is resolvable through the prototype chain, for example:</p> <pre><code>bar.hasOwnProperty('one'); // true, the property is "own" bar.hasOwnProperty('two'); // false, the property is "inherited" from foo bar.two; // 2, is accessible </code></pre> <p>Basically, the prototype chain of <code>bar</code> looks like this:</p> <pre> ----------------- ========> | Object.prototype| ==> null | ----------------- |-------------| [[Prototype]] |---------| | one: 100 | ====================> | one: 1 | (shadowed) | three: 3 | | two: 2 | |-------------| |---------| (== line denotes the prototype chain) </pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload