2015年8月19日 星期三

javascript class global variable example

http://stackoverflow.com/questions/1535631/static-variables-in-javascript

function MyClass () { // constructor function
  var privateVariable = "foo";  // Private variable 

  this.publicVariable = "bar";  // Public variable 

  this.privilegedMethod = function () {  // Public Method
    alert(privateVariable);
  };
}

// Instance method will be available to all instance but only load once in memory 
MyClass.prototype.publicMethod = function () {    
  alert(this.publicVariable);
};

// Static variable shared by all instance 
MyClass.staticProperty = "baz";

//...
var myInstance = new MyClass();




沒有留言:

張貼留言