Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The technical reason why those methods are hidden is twofold. </p> <p>First, when you execute a method on the Test object, "this" will be the untyped object returned at the end of the anonymous function that contains the public methods per the <a href="http://yuiblog.com/blog/2007/06/12/module-pattern/" rel="nofollow noreferrer">Module Pattern</a>. </p> <p>Second, the methods testOne, testTwo, and testThree aren't attached to a specific object, and exist only in the context of the anonymous function. You could attach the methods to an internal object and then expose them through a public method, but it wouldn't be quite as clean as the original pattern and it won't help if you're getting this code from a third party.</p> <p>The result would look something like this:</p> <pre><code>var Test = function() { var private = { testOne : function () {}, testTwo : function () {}, testThree : function () {} }; function getMethods() { for (i in this) { alert(i); // shows getMethods, but not private methods } for (i in private) { alert(i); // private methods } } return { getMethods : getMethods } }(); // will return ['getMethods', 'testOne', 'testTwo', 'testThree'] Test.getMethods(); </code></pre> <p><strong>edit:</strong></p> <p>Unfortunately, no. The set of local variables aren't accessible via a single, automatic keyword. </p> <p>If you remove the "var" keyword they would be attached to the global context (usually the window object), but that's the only behavior that I know of that is similar to what you're describing. There would be a lot of other properties and methods on that object if you did that, though.</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