Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are no protected variables/properties in JavaScript. Though, you can reuse "private" variables when you declare the inheriting classes in the same scope, which seems possible in your case when the private variables are only "hidden utilities" of your prototype.</p> <pre><code>MyNamespace.Person = function Person(params) { // private variables and functions, individual for each Person instance var anything, id; function execute_something() {} // public properties: this.name = ""; this.getId = function getId(){ // called a "privileged function", because it has access to private variables } } MyNamespace.American = function(params) { MyNamespace.Person.call(this, params); // inherit name and getId() } (function() { // new scope for // hidden utility functions and other private things function foo() { } function helpJSON() { } function fromJSON() { } var bar; (function(personProto) { // new scope for prototype module (not explicitly needed) // "private" /static/ variables (and functions, if you want them private) var personCount = 0; personProto.clone = function clone() { return this.constructor(myself); // or something }; personProto.toJSON = function toJSON() { // use of helpJSON() }; personProto.fromJSON = fromJSON; // direct use })(MyNamespace.Person.prototype); (function(amiProto) { // just the same as above, if needed amiProto.special = function() { // use foo() and co }; })( MyNamespace.American.prototype = Object.create(MyNamespace.Person.prototype) ); })(); </code></pre> <p>This is the JavaScript way of inheritance, which means American's prototype inherits the clone(), toJSON() and fromJSON() functions automagically from the Person's prototype. Of course overwritable. And the feature is</p> <pre><code>new MyNamespace.American() instanceof MyNamespace.Person; // true </code></pre> <p>Of course, if you don't need that, and want use the more module-like way, you could reuse the utility functions, i.e. just copy them:</p> <pre><code>(function() { // hidden utility functions and other private things var bar; var personCount; function foo() { } function helpJSON() { } function fromJSON() { } function clone() { return this.constructor(myself); // or something } function toJSON() { } (function(personProto) { // new scope, not really needed // private variables are useless in here personProto.clone = clone; personProto.toJSON = toJSON; personProto.fromJSON = fromJSON; })(MyNamespace.Person.prototype); (function(amiProto) { // new scope, not really needed // copied from personProto amiProto.clone = clone; amiProto.toJSON = toJSON; amiProto.fromJSON = fromJSON; // and now the differences amiProto.special = function() { // use foo() and co }; })(MyNamespace.American.prototype); })(); </code></pre>
    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