Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, sub-classing in JS is typically a bad idea, because people think that they're getting extension, where every instance has its own copy of properties and methods...</p> <p>...really, they're getting <code>public static</code> access to the parent's stuff.</p> <p>Even better, that <code>public static</code> stuff has no access to the encapsulated variables, so there's really no manipulation of private data, unless you're using private functions (with a public interface) to pass data to and collect return values from, the public static stuff. </p> <pre><code>var Parent = function () { this.static_prop = 0; this.static_method = function (num) { this.static_prop += 1; return num + this.static_prop; }; }; var Child = function (num) { this.public_func = function () { num = this.static_method(num); }; }; Child.prototype = new Parent(); var child = new Child(13); child.public_func(); </code></pre> <p>Just calling <code>this.static_method</code> wouldn't help, because it would have 0 access to <code>num</code>, which means that you're wrapping things which you inherited to grant them access to use private data as inputs, which means that you're doing most of the writing you'd be doing anyway, regardless of inheritance, because your expectations of <code>.prototype</code> were backwards. </p> <p>Might I suggest Dependency Injection, instead?<br> Component-based programs?</p> <pre><code>var Iterator = function () { var count = 0, min = 0, max = 0, reset = function () { count = min; }, next = function () { count = count &gt;= max ? min : count; return count += 1; }, set_min = function (val) { min = val; }, set_max = function (val) { max = val; }, public_interface = { reset : reset, count : count, set_min : set_min, set_max : set_max }; return public_interface; }, Thing = function (iter) { var arr = [], currentObj = null, nextObj = function () { currentObj = arr[iter.next()]; }, add = function (obj) { arr.push(obj); iter.set_max(arr.length); }, public_interface = { next : nextObj, add : add }; return public_interface; }; var thing = Thing(Iterator()); thing.add({}); thing.next(); </code></pre> <p>It's a convoluted example, but now every instance is going to be given exactly what it needs to do its job (because the constructor requires it -- or you can add the dependency later, through a public method, or as a public-property).</p> <p>The interfaces for each module can now also get as simple and as clean as you'd like, as you don't have to wrap unexpected static-helpers to get private data... </p> <p>Now you know what's private, you know what you're extending to the public, and you have clean ins and outs wherever you want to put them.</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. 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