I recently learned a little more about nesting functions and discovered that I'm writing inefficient code by defining methods right inside a constructor: this.method=function(){}
and read that it would be more efficient to use: constructor.prototype.method=function(){}
.
However, Before I was writing 'private' properties as local variables and defining getters within the constructor:
function class(prop)
{
var prop2=prop*2;
this.__defineGetter__('prop2',function() {return prop2;});
}
But I quickly realized that removing the second line in the constructor and using class.prototype.__defineGetter__('prop2',function() {return prop2;});
returns a blank string
Is there maybe a way to fix this? or an altogether better way to make properties in an object only accessable by abstraction?