Skip navigation

Monthly Archives: May 2009


function Man(name) {

// private property
var lover = “simron”;

// public property
this.wife = “rocy”;

// privileged method
this.getLover = function(){return lover};

// public method
Man.prototype.getWife = function(){return this.wife;};

}

// static property
Man.mother = “teresa”;

Now creating instance & checking access

// create a new instance
var emp = new Man(“raja”);

// access private property
emp.lover;   // undefined

// access public property
emp.wife;  // rocy

// access privileged method
emp.getLover();   // simron

// access public method
emp.getWife();  // rocy

// access static property
Man.mother;  // teresa

Need one more example??

var Female = (function() {
   var _trueAge = 48,
        _trueWeight = 125;

   return {
      age : _trueAge - 15,
      weight : _trueWeight - 30
   };
})();

Female.age; // 33
Female.weight; // 95
Female._trueAge; // undefined