Javascript: vars instances and static methods; is this normal 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 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();
+3
source share
1 answer

, , , .
-, ... . , .

, , :

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

, "":)).

"" , , , , , ( - , "this" , )

.

"" , , . 2 , , .

:

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

, :

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

, - .

( "" ):

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");

staticMethod Vehicle:

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

, staticMethod, , ​​ , .
"" .

P.S.: , :)

+1

Source: https://habr.com/ru/post/1782491/


All Articles