<p>Here is some example code. For detailed see <a href="http://javascript.crockford.com/private.html">this</a></p>
<pre data-sub="prettyprint:_">
function User()
{
var name = "fred";
this.privillegedShowName = function()
{
alert(name);
}
}
User.prototype.publicShowName = function()
{
this.privillegedShowName();
//alert(this.name); //failed
}
var u = new User();
u.privillegedShowName();
u.publicShowName();
//alert(u.name); //failed
</pre>
<p>
In essence, privileged method are dynamically generated methods, because they'are added to the object at runtime, rather than when the code is first complied. While this technique is also much more powerful and flexible. Here are some example.
</p>
<pre data-sub="prettyprint:_">
// Create a new user object that accepts an object of properties
function User( properties )
{
// Iterate through the properties of the object, and make sure
// that it's properly scoped (as discussed previously)
for ( var i in properties )
{
//begin of anonymous method
(function()
{
// Create a new getter for the property
this[ "get" + i ] = function()
{
return properties[i];
};
// Create a new setter for the property
this[ "set" + i ] = function(val)
{
properties[i] = val;
};
})();
//end of anonymous method
}
}
// Create a new user object instance and pass in an object of
// properties to seed it with
var user = new User({name: "Bob",age: 44});
// Just note that the name property does not exist, as it's private
// within the properties object
alert( user.name == null );
// However, we're able to access its value using the new getname()
// method, that was dynamically generated
alert( user.getname() == "Bob" );
// Finally, we can see that it's possible to set and get the age using
// the newly generated functions
user.setage( 22 );
alert( user.getage() == 22 );
</pre>