accessing private properties in javascript object

Parvendra
Posted by Parvendra under JavaScript category on | Views : 1799
To access local variables create local functions within the scope of the constructor of main function

//define a function
function testpriv()
{
var local_var = 7;

this.GetPrivVar = function()
{
return local_var;
}
}

//make object of the function to initialize constructor
obj1 = new testpriv;

//access provate property by
document.write(obj1.GetPrivVar());

//this line will give error saying 'local_var is not defined'
document.write(local_var);

Comments or Responses

Login to post response