Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In the first one you can only create the object once, while with the second one you can create as many objects as you like. I.E. the first one is effectively a singleton.</p> <p>Note that closures are not ok for the second one. Every time you instantiate it you are creating the functions all over again and waste a ton of memory. The prototype object is intended to counter this, where you can create the functions once outside a function scope and no accidental closures are created.</p> <pre><code>function foo2(){ this._bar = 0; } foo2.prototype = { constructor: foo2, getBar: function(){ return this._bar; }, addOne: function(){ this._bar++; }, addRandom:function(rand){ this._bar += rand; } }; </code></pre> <p>Then:</p> <pre><code>var a = new foo2, b = new foo2, c = new foo2; </code></pre> <p>Creates three instances which have their own <code>_bar</code> but share the same functionality.</p> <p><a href="http://jsperf.com/object-literal-vs-new-operate/4" rel="nofollow noreferrer">jsperf</a></p> <p>You can "compare" all of this to PHP, some of the code won't even run but it's "equivalent" in principle:</p> <hr> <pre><code>var foo = (function(){ var bar = 0; return { getBar: function(){ return bar; }, addOne: function(){ bar++; }, addRandom: function(rand){ bar += rand; } } })(); </code></pre> <p>is roughly "equivalent" to this in PHP:</p> <pre><code>$foo = new stdClass; $foo-&gt;bar = 0; $foo-&gt;getBar = function(){ return $this-&gt;bar; }; $foo-&gt;addOne = function(){ $this-&gt;bar++; } $foo-&gt;addRandom = function($rand){ $this-&gt;bar += $rand; } </code></pre> <hr> <pre><code>var foo2 = function(){ var bar = 0; this.getBar = function(){ return bar; }; this.addOne = function(){ bar++; }; this.addRandom = function(rand){ bar += rand; } }; </code></pre> <p>Is roughly "equivalent" to this in PHP:</p> <pre><code>Class foo2 { public function __construct(){ $bar = 0; $this-&gt;getBar = function(){ return $bar; }; $this-&gt;addOne = function(){ $bar++; }; $this-&gt;addRandom = function($rand){ $bar += rand; }; } } </code></pre> <hr> <pre><code>function foo2(){ this._bar = 0; } foo2.prototype = { constructor: foo2, getBar: function(){ return this._bar; }, addOne: function(){ this._bar++; }, addRandom:function(rand){ this._bar += rand; } }; </code></pre> <p>Is roughly "equivalent" to this in PHP:</p> <pre><code>Class foo2 { public $_bar; public function __construct(){ $this-&gt;_bar = 0; } public function getBar(){ return $this-&gt;_bar; } public function addOne(){ $this-&gt;_bar++ } public function addRandom($rand){ $this-&gt;_bar += $rand; } } </code></pre> <p>...and is the only one that is close to OOP in the three above examples</p> <hr>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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