I'm trying to dynamically generate a prototype by adding properties in order to save time. My wish is to generate the prototype only once as the generation process is time consuming, and then use this prototype for another Object.
// Generated prototype
var PROTO = {
a: "a",
b: "b",
data : { // Object dynamically added through time-consuming function
c: "c",
d: "d"
}
}
function OBJ() {}
OBJ.prototype = PROTO;
var obj1 = new OBJ();
var obj2 = new OBJ();
PRINT1();
obj1.a = "non a";
obj1.data.c = "non c";
PRINT2();
At PRINT1 the output is
obj1: (a, b, c, d), obj2: (a, b, c, d)
At PRINT2 the output is
obj1: (non a, b, non c, d), obj2: (a, b, non c, d)
I understand that both obj1 and obj2 shares the same reference to data object, but it's not how prototypes should work in the concept...
I tried also this way, but it didn't work.
function PROTO () { ... }
[...]
OBJ.prototype = new PROTO();
How can I have objects with same internal structure taken from a generated prototype?