Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Have you considered a closure, instead?</p> <p>For example:</p> <pre><code>var myExtension = ( function() { var me = {}; var private_thingy = "abcDEFG123"; var onPageLoad = function() { // do page loading business alert( private_thingy ); } me.onPageLoad = onPageLoad; var init = function() { onPageLoad(); } me.init = init; return me; })(); /////////////// myExtension.private_thingy = "DIDDLED!"; // will NOT modify the private_thingy declared within the closure myExtension.init(); // will work fine. </code></pre> <p>Anything you define within the closure is available within the closure at all times, and when implemented carefully will yield private members not accessible to users of the object. Only members that you explicitly export - e.g., the me.xxx = xxx lines - are publicly available.</p> <p>Thus, when onPageLoad executes, "abcDEFG123" will be displayed in the alert, <em>not</em> "DIDDLED!". Users of the object can modify properties using the dot operator until the cows come home; what's not made public by the closure, however, can never be modified: even if the user reassigns a function on the public interface, calls to the private function from within the closure will always point to the function defined within the closure.</p> <p>The important part: it unties you from the constant use of <em>this</em> (unless you really want to use it; save your fingers for more important typing!).</p> <p>Give it a shot. And have a look at <em>Javascript: The Good Parts</em> by Crockford.</p>
    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. This table or related slice is empty.
    1. This table or related slice is empty.
    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