Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You might take advantage of the fact that JS functions are also objects -- which means they can have properties.</p> <p>For instance, quoting the example given on the (now vanished) article <a href="http://web.archive.org/web/20120502014437/http://www.tipstrs.com/tip/1084/Static-variables-in-Javascript" rel="noreferrer">Static variables in Javascript</a>:</p> <pre><code>function countMyself() { // Check to see if the counter has been initialized if ( typeof countMyself.counter == 'undefined' ) { // It has not... perform the initialization countMyself.counter = 0; } // Do something stupid to indicate the value alert(++countMyself.counter); } </code></pre> <p>If you call that function several time, you'll see the counter is being incremented.</p> <p>And this is probably a much better solution than poluting the global namespace with a global variable.</p> <p><br> And here is another possible solution, based on a closure : <a href="http://chamnapchhorn.blogspot.com/2008/07/trick-to-use-static-variables-in.html" rel="noreferrer">Trick to use static variables in javascript</a> : </p> <pre><code>var uniqueID = (function() { var id = 0; // This is the private persistent value // The outer function returns a nested function that has access // to the persistent value. It is this nested function we're storing // in the variable uniqueID above. return function() { return id++; }; // Return and increment })(); // Invoke the outer function after defining it. </code></pre> <p>Which gets you the same kind of result -- except, this time, the incremented value is returned, instead of displayed.</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. 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