Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>Is there any difference between those two?</p> </blockquote> <p>One major difference is that in the second case using <em>new</em>, the returned object has an additional object on it's <code>[[Prototype]]</code> chain that is the public prototype of the constructor function expression. That object can be accessed in some browsers using <code>__proto__</code>.</p> <p>Also, <code>myObject1.constructor</code> is the built–in Object function, while <code>myObject2.constructor</code> is the function created by the function expression.</p> <blockquote> <p>Are there any other possibilities to implement private members?</p> </blockquote> <p>I suppose you could use the object's (otherwise useless) <code>__proto__</code> object, but that's more obfuscation than privacy (and not available in some browsers):</p> <pre><code>var foo = new function() { this.__proto__.privateMember = 42; this.publicMember = function() { return this.__proto__.privateMember; } }; alert(foo.publicMember()); </code></pre> <p>Pretty ugly really.</p> <p>Closures have been used to emulate private members in javascript for quite some time (more than 10 years at least) but there hasn't been any real pressure to include them in ECMA-262 (as far as I'm aware). I suppose that indicates that private members are handy, but not critical to implement functionality required for the type of host environment scripting that javascript is typically used for.</p> <h3>Edit</h3> <p>As Bergi says, the ES5 <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.3.2" rel="nofollow"><em>Object.getPrototypeOf</em></a> method should be used in stead of the (non standard) <a href="https://developer.mozilla.org/en/docs/JavaScript/Reference/Global_Objects/Object/proto" rel="nofollow"><code>__proto__</code></a> property. My bad for no pointing that out initially.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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