Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>An interesting question. As for #1 and #2: let's say you have a function foo:</p> <pre><code>function foo() { var _private = 'private var!'; this.access = function () { return _private; } } </code></pre> <p><code>access</code> is a so-called privileged method, it's a closure that can access the private variable <code>private</code>.</p> <p>you can inherit the whole thing by making use of <code>call</code>, like so:</p> <pre><code>function bar() { foo.call(this); } var b = new bar(); console.log(b.output()); // prints 'private var!' </code></pre> <p>With the methods <code>apply</code>, <code>call</code> and <code>bind</code> you can establish the context of a function, effectively tamper with the <code>this</code> object. (your #2 question, read <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call" rel="nofollow">here</a> )</p> <p>Naturally you cannot make use of a totally private method in a derived object. You'd need an accessor method which would defeat the purpose of the original method being private. Having said that, that's the way it works in strongly typed languages too (in java if you mark a method as private not even subclases will be able to access it, it would have to be protected).</p> <p>As for #3, I cannot think of how to keep callbacks shared and private. But you can make it a static property for all instances of a function (much like a static property in a lanaguage like java) by simply declaring a function like:</p> <pre><code>function foo() { } </code></pre> <p>add your prototypes which will be assigned to each instance</p> <pre><code>foo.prototype.bar = // ... </code></pre> <p>and a static property</p> <pre><code>foo.callbacks = []; </code></pre> <p>All instances of foo will share the callbacks property.</p>
 

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