if (typeof Object.create4 !== 'function') {
Object.create4 = function (t) {
var F, f, i, ins = {}, sta = {};
for(i in t){
if(typeof t[i] === 'function'){
sta[i] = t[i];
}
else{
ins[i] = t[i];
}
}
ins = jQuery.extend(true, {}, ins);
F = function() {}
F.prototype = sta;
f = new F();
for(i in ins){
f[i] = ins[i];
}
return f;
};
}
var Vehicle4 = (function(){
var that = {}
that.instanceVar = {hey: 1}
that.staticMethod = function(){
console.log(this.instanceVar);
}
return that;
}())
var v31 = Object.create4(Vehicle4);
var v32 = Object.create4(Vehicle4);
v31.instanceVar.hey = 2;
v31.staticMethod();
v32.staticMethod();
Is this normal in terms of memory? I mean: in the 1000 objects that will be installed:
1 * STATICMETHOD 1000 * instanceVar
effective? I want to note that instanceVar will be changed in every object, so the signle object will not be sufficient.
and may have any memory leaks?
var inherit = function(P, C) {
return jQuery.extend(true, {}, P, C);
}
var Vehicle = function() {}
Vehicle.prototype = {
init: function(){
this.instanceVar = {hey: 1}
},
staticMethod: function() {
console.log(this.instanceMember);
},
staticMethod3: function() {
console.log(this.instanceMember);
}
}
var SuperVehicle = function() {}
SuperVehicle.prototype = inherit(Vehicle.prototype, {
init: function(){
this.super.init.call(this);
this.instanceVar2 = {hey: 1}
},
staticMethod: function() {
console.log(this.instanceVar.hey);
console.log(this.instanceVar2.hey);
},
staticMethod2: function() {
console.log(this.instanceVar.hey);
console.log(this.instanceVar2.hey);
}
});
SuperVehicle.prototype.super = Vehicle.prototype;
var s = new SuperVehicle();
s.init();
s.staticMethod();
s.staticMethod2();
s.staticMethod3();
source
share