Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'll start with the way I usually define classes:</p> <pre><code>function CustomDataType(a) { this.a = a; this.b = 2; } CustomDataType.prototype = { doX : function () {} }; var obj = new CustomDataType(1); </code></pre> <p>I assign variables in the constructor because <code>F.prototype = { a : [1,2,3]; }</code> is problematic, the array will be shared between instances unless the property is reinitialized in the constructor (problematic for all non-primitive types). And it's also needed when the property somehow depends on the arguments to the constructor. I declare methods in the prototype so that they are shared between all instances, if you use inheritance it means you can have super calls, it will also lead to less memory usage since the method only has to be allocated once (though that probably isn't a big issue).</p> <p>I have never used <code>Object.create</code>, I don't see any reason to do to that unless you are into some insane dynamic hackery.</p> <p>I use object literals whenever it feels unnecessary to create a separate class for the objects, for instance if it's arguments to a methods (so callers don't have to explicitly instantiate something), or if it's some internal values that shouldn't be accessed by anyone else.</p> <p>Your factory is essentially the same as the object literal. I suppose it can be useful if you need to create some jsonesque structure and set some default values.</p> <p>I don't see why <code>new</code> would be dangerous or unnecessary, in what context did he say that?</p> <p>But I think a lot of it comes down to taste. Be consistent and don't complicate things.</p>
    singulars
    1. This table or related slice is empty.
    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.
    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