Learning Javascript; I want to reduce memory usage by using a prototype function (#2). However, in order to pass relevant state/arguments from an instance to the prototype function, I need to create another function (#1).
I understand that in Javascript, the object method (#1) would be created for each Row instance, negating memory savings from re-using the prototype function (#2). Memory savings would also be negated if I replaced function #1 with a closure.
Is there a way for each Row object to call the prototype function on the Row's own unique state while still minimizing memory usage?
function Row(data) {
row = Object.create(Row.prototype);
row.state = data;
//#1
row.showInstanceState = function() {
Row.prototype.showState(this.state);
};
return row;
}
//#2
Row.prototype.showState = function(info) {
console.log(info);
}
let example = new Row(2);
/*
If function #1 didn't exist, the call
below saves memory but we have explicitly pass
in an instance's data at the moment of the call.
*/
example.showState(example.state);
//The call style below is desired, but requires function #1, which would not optimize memory usage.
example.showInstanceState();