Skip navigation

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 
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: