Quantcast
Channel: Daz Script Developer Discussion - Daz 3D Forums
Viewing all articles
Browse latest Browse all 1036

Static Variable for a Function?

$
0
0

Hi,

I understand it's possible to create static variables in Javascript.  Because functions are considered to be objects, it's possible to have something like this:

(function() {

  function doSomething(param) {
    if (doSomething.x == undefined)
      doSomething.x = 1;  // this value is retained between calls
  };

  doSomething.x = undefined;
  
})()

 

Now, I'm trying to setup my own library of functions, and I'm told that it's best to define them in a separate namespace instead of putting them in the global namespace.

So for the above example, I'm doing the following (which uses Javascript's 'other' syntax where you define first the property and then the value):

var MyLibrary = {
  stuff : true,
  
  stuff2 : "hello",
  
  doSomething : function(param) {
    if (doSomething.x == undefined) {
      doSomething.x = 1;  // this value is retained between calls
    }
  },
	  
  doSomething.x : undefined  // this doesn't work - why??
};

 

The trouble with this 'other' syntax is I can't figure out how to declare the static variable.  Does anyone know how?

 


Viewing all articles
Browse latest Browse all 1036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>