I have a JavaScript object that does something like this - using closure to simulate private vs public functions / variables:
var myCoolObject = function(x, y) { var prop1 = "a cool prop1 value"; var negX = x * -1; var negY = y * -1; var xyProduct = x * y; return { PublicProp1: prop1, getXYProduct: function() { return xyProduct; }, getNegX: function() { return negX; }, getNegY: function() { return negY; } } }
I will create about 4000 instances of this object, and from what I read, adding functions via prototype will be more efficient than adding them, as I above (because in my example each instance will have its own getXYProcust() , getNegX() and getNegY() .
My question is twofold - is my approach above really "ineffective"? I understand that an ineffective relative term is what Iām most likely to notice. If this is inefficient, how would I add these functions to the prototype from myCoolObject ? I tried the following:
myCoolObject.prototype.protoProp = "pppp"; myCoolObject.prototype.getAtMeBro = function () { return "get at me bro"; }; var myInstance = new myCoolObject(5, 10);
But neither protoProp nor 'getAtMeBro ()' are properties of myInstance when I check it.
Thanks in advance for any help - I appreciate it!
Mattw
source share