Static Variable for a Function?

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?

 

Comments

  • andya_b341b7c5f5andya_b341b7c5f5 Posts: 568
    edited August 25

    The variable doSomething.x isn't accessible until you have finished defining the object MyLibrary.  This works:

    var MyLibrary = {
      stuff : true,
      
      stuff2 : "hello",
      
      doSomething : function(param) {
      	var z = 'bar';
    
        if (MyLibrary.doSomething.x == undefined) {
          MyLibrary.doSomething.x = 1;  // this value is retained between calls
        }
        MyLibrary.doSomething.y = 'foo';
      },
      
      //doSomething.x = undefined  // this doesn't work - why?? - doesn't exist
      //MyLibrary.doSomething.x = undefined  // this doesn't work either, same reason
      
    };
    
    // Function variables undefined before you call the function
    
    f = MyLibrary.doSomething
    print(f.x);  // undefined
    print(f.y);  // undefined
    print(f.z);  // undefined
    
    f();
    print(f.x, f.y);  // 1 foo
    print(f.z);      // undefined - local to function

     

     

    Post edited by andya_b341b7c5f5 on
  • Stretch65Stretch65 Posts: 107

    Thanks.  Javascript isn't exactly my favourite language :)

  • Stretch65 said:

    Thanks.  Javascript isn't exactly my favourite language :)

    Not mine eiither!  I'm baffled by it's popularity outside of web browsers.

  • TotteTotte Posts: 10,673

    ECMAscript do have some percuilar things,my favourite traps is still

    function stupid(a,b) {

    c=a+b;

    printf(c);
    }

    a = 1;
    b = someObjectArray[someindex].somemember;

    stupid(a,b);

    result >> 11

  • Stretch65Stretch65 Posts: 107
    Totte said:

    ECMAscript do have some percuilar things,my favourite traps is still

    function stupid(a,b) {

    c=a+b;

    printf(c);
    }

    a = 1;
    b = someObjectArray[someindex].somemember;

    stupid(a,b);

    result >> 11

    Really?  Lol .. Now I hate this language even more.

Sign In or Register to comment.