After reviewing insights from Google developers, I came across a helpful article on optimizing JavaScript at this link.
The recommendation is to place the declaration and initialization of instance variables with value type on the prototype. This prevents the unnecessary running of initialization code every time the constructor is called. However, this approach may not apply if the initial value of instance variables depends on constructor arguments or other states during construction.
For example, instead of:
foo.Bar = function() {
this.prop1_ = 4;
this.prop2_ = true;
this.prop3_ = [];
this.prop4_ = 'blah';
};
It is suggested to use:
foo.Bar = function() {
this.prop3_ = [];
};
foo.Bar.prototype.prop1_ = 4;
foo.Bar.prototype.prop2_ = true;
foo.Bar.prototype.prop4_ = 'blah';
I understand the benefits of placing value type variables in the function prototype. However, my concern is whether initializing reference variables like this.prop3_ = []; (as shown in the Google example) creates a new array with each constructor invocation?