Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes it's possible to define privileged methods outside of the constructor. Here is how to do it:</p> <pre><code>var Keeper = (function (key) { function Keeper(get) { var private = { secretPower: "wisdom" }; this.getPrivate = function (k) { if (k === key) return private; }; this.get = get || defaultGet; } function defaultGet() { var private = this.getPrivate(key); return "The secret power is: " + private.secretPower; } return Keeper; }({})); </code></pre> <p>Here is how it works:</p> <ol> <li>We create a namespace by creating an <a href="http://benalman.com/news/2010/11/immediately-invoked-function-expression/" rel="nofollow" title="Ben Alman &raquo; Immediately-Invoked Function Expression (IIFE)">Immediately Invoked Function Expression (IIFE)</a>.</li> <li>We create a object called <code>key</code>. This object is private to the namespace we just created. Hence only privileged functions and the constructor can access it.</li> <li>Inside the constructor we create an object called <code>private</code> which holds the private state of each instance.</li> <li>We also create a privileged method called <code>getPrivate</code> which takes an argument <code>k</code> and only returns the private object if <code>k</code> is the <code>key</code> we created in step 2. Hence only privileged functions can access the private object.</li> <li>Privileged functions which want to access the private state of the object must call <code>getPrivate(key)</code> to get the private state object.</li> </ol> <p><strong>Note:</strong> This method is only useful if you have more than one privileged function which needs to access the private state of the object. In addition the call the <code>getPrivate</code> introduces a (very) small performance overhead to privileged functions.</p> <hr> <p>BTW if you want to write clean object-oriented code in JavaScript then take a look at the <a href="https://github.com/javascript/augment" rel="nofollow" title="javascript/augment"><code>augment</code></a> library. The above code will looks like this using <code>augment</code>:</p> <pre><code>var Keeper = Object.augment(function (key) { this.constructor = function (get) { var private = { secretPower: "wisdom" }; this.getPrivate = function (k) { if (k === key) return private; }; this.get = get || defaultGet; }; function defaultGet() { var private = this.getPrivate(key); return "The secret power is: " + private.secretPower; } }, {}); </code></pre>
    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