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?
_myprivatewould show other programmers and your future self that you should not access them directly. You can't have methods on the prototype and access instance specific private variables, you can have shared private members on the prototype, some patters and more detail can be found here: stackoverflow.com/a/16063711/1641941