I have an object in Javascript and want to process the data passed into the constructor using a function that could later be called externally with more data. Naturally I don't want to duplicate the code (once in the constructor, once in a function), so how should I best set this up?
I could use a nested function, but I'm told this is inefficient:
function MyOb(data) {
this.myData = {};
function addData(newData) {
//Add newData to myData
}
addData(data);
}
But if I use a prototype I get a "can't find variable addData" error on line 3:
function MyOb(data) {
this.myData = {};
addData(data);
}
MyOb.prototype.addData = function(newData) {
//Add newData to myData
}
So am I forced to either use a nested function or repeat myself, or is there a way of making this work using prototype?