Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: instance vars and static methods; is this ok in terms of memory?

if (typeof Object.create4 !== 'function') {
    Object.create4 = function (t) {
        var F, f, i, ins = {}, sta = {};

        for(i in t){
            // method: static, means will only exists 1, so is less memory intensive
            if(typeof t[i] === 'function'){
                sta[i] = t[i];
            }
            // vars: instance, means 1 for each object, so is more memory intensive
            else{
                ins[i] = t[i];
            }
        }

        // make a copy of the instances
        ins = jQuery.extend(true, {}, ins);


        F = function() {}
        F.prototype = sta;
        f = new F();

        // assign instances to the instance
        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 ok in terms of memory? I mean: in 1000 objects instanced there will be:

1*staticMethod 1000*instanceVar

is this efficient? I want to note that the instanceVar will be modified in each object so a signle object is not enought.

and might 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();
like image 974
Totty.js Avatar asked Jun 04 '26 18:06

Totty.js


1 Answers

I can tell you for sure that it is correct and you won't have memory leaks, but concerning the efficiency, i have some doubts.
Firstly, your static members aren't quite static... they are just added to the prototypic chain of the object. The whole prototype inheritance system relies on the fact that every object inherits it's fathers prototype recursively.

So, if you add a property to the primitive Object , such as:

Object.prototype.toString = function(){console.log("I am a primitive object");}

all of the object in your window would inherit this function and they would be "primitive" :)).

You could call this a "static" method only if you consider the fact that it is loaded in the memory only once and not for every instance, but you cannot consider it static, because it interacts with the current instance of the object (in other object oriented languages, if you put the "this" keyword inside a static method, it throws an exception)

But I don't see the point of all that in your example.

In your example, you pin the "static" methods into the prototype of the object you want to create, but you recreate the prototype for each object instance. If you create 2 or more instances of the same "class", they won't share the prototype but they will each have identical ones.

It's right there:

F = function() {};
F.prototype = sta;
f = new F();

Every time you create vehicles with this method:

var myVehicle = Object.create4(Vehicle4);
var anotherVehicle = Object.create4(Vehicle4);

You create prototypes for each instance, witch kind of defeats the purpose of the prototypic inheritance.

I would definitely go for the classic method of creating objects (with the "new" operator) :

var Vehicle = function(val){this.instanceMember = val;}  
Vehicle.prototype = {   
    "staticMethod": function(){console.log(this.instanceMember);}   
}    
var v1 = new Vehicle("foo");  
var v2 = new Vehicle("bar");

This way you can easily change the staticMethod and affect all Vehicle instances:

Vehicle.prototype.staticMethod = function(){  
    console.log(arguments[0] || this.instanceMember);  
};

while in your example if you change the staticMethod, the changes will be applied only to the instances constructed after the change occurred.
This being said, in this case, the old classic way of creating objects with "static" members is by far more efficient.

P.S. : Sorry if i got carried away :)

like image 170
gion_13 Avatar answered Jun 07 '26 06:06

gion_13