I usually extend constructor functions with the .prototype
property, to have multiple instances reference the same code (instead of having several identical code), like this:
function Effect(){}
Effect.prototype.run = function(){}
However, due to the structure of my current project, I would like to do something like this:
Define effects
object, with effect options:
var effects = {
e1 : {
run: function(){console.log(this);}
},
e2 : {
run: function(){console.log(this);}
}
}
Create Effect instance using the object definitions
function Effect(effect) {
this.run = effect.run;
}
Now I can create several instances (maybe hundreds)
var E1 = new Effect(effects.e1);
var E2 = new Effect(effects.e2);
var E3 = new Effect(effects.e2);
.....
However I'm concerned, if the .run
functions will be defined multiple times.
Will these new instances reference the same function (defined in the effects
object), or will they create a new 'local' function for each instance, using up memory space?