Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You are confusing the <code>prototype</code> property that can be used on <a href="https://developer.mozilla.org/En/Core_JavaScript_1.5_Guide/Creating_New_Objects/Using_a_Constructor_Function" rel="noreferrer">Constructor Functions</a> and the <em>internal</em> <code>[[Prototype]]</code> property.</p> <p>All objects have this internal <code>[[Prototype]]</code> property, and only the <a href="http://bclary.com/2004/11/07/#a-11.2.2" rel="noreferrer"><code>new</code></a> operator when you call it with a constructor function is allowed to set it (through the <a href="http://bclary.com/2004/11/07/#a-13.2.2" rel="noreferrer"><code>[[Construct]]</code></a> internal operation).</p> <p>If you want to have prototypal inheritance with object instances (without using constructors), the Crockford's <a href="http://javascript.crockford.com/prototypal.html" rel="noreferrer"><code>Object.create</code></a> technique is what you want (that method is now part of the recently approved ECMAScript 5th Edition):</p> <pre><code>// Check if native implementation available if (typeof Object.create !== 'function') { Object.create = function (o) { function F() {} // empty constructor F.prototype = o; // set base object as prototype return new F(); // return empty object with right [[Prototype]] }; } var confProto = { d: 16 }; var conf = Object.create(confProto); conf.a = 2; conf.b = 4; </code></pre> <p>In the above code <code>conf</code> will have its three members, but only <code>a</code> and <code>b</code> will exists physically on it:</p> <pre><code>conf.hasOwnProperty('a'); // true conf.hasOwnProperty('b'); // true conf.hasOwnProperty('d'); // false </code></pre> <p>Because <code>d</code> exists on the conf <code>[[Prototype]]</code> (<code>confProto</code>).</p> <p>The <a href="http://bclary.com/2004/11/07/#a-11.2.1" rel="noreferrer">property accessors</a>, <code>.</code> and <code>[]</code> are responsible to resolve the properties looking up if necessary in the prototype chain (through the <a href="http://bclary.com/2004/11/07/#a-8.6.2.1" rel="noreferrer"><code>[[Get]]</code></a> internal method).</p>
 

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