gpt4 book ai didi

Javascript:实例变量和静态方法;这在内存方面可以吗?

转载 作者:行者123 更新时间:2023-11-29 15:51:45 25 4
gpt4 key购买 nike

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

这在内存方面可以吗?我是说:在实例化的 1000 个对象中将有:

1*静态方法1000*实例变量

这样有效率吗?我想指出,instanceVar 将在每个对象中被修改,所以一个单一的对象是不够的。

并且可能有任何内存泄漏?

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

最佳答案

我可以肯定的告诉你,它是正确的,你不会有内存泄漏,但是关于效率,我有些怀疑。
首先,您的静态成员不是完全静态的……它们只是添加到对象的原型(prototype)链中。整个原型(prototype)继承系统依赖于这样一个事实,即每个对象递归地继承它的父亲原型(prototype)。

因此,如果您向原始对象添加一个属性,例如:

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

您窗口中的所有对象都将继承此函数,并且它们将是“原始的”:))。

只有当你考虑到它只在内存中加载一次而不是为每个实例加载这一事实时,你才可以称它为“静态”方法,但你不能认为它是静态的,因为它与对象的当前实例交互(在其他面向对象的语言中,如果将“this”关键字放在静态方法中,它会抛出异常)

但我看不到您的示例中所有这些的意义。

在您的示例中,您将“静态”方法固定到要创建的对象的原型(prototype)中,但您为每个对象实例重新创建了原型(prototype)。如果您创建相同“类”的 2 个或更多实例,它们将不会共享原型(prototype),但它们每个都有相同的原型(prototype)。

它就在那里:

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

每次使用这种方法创建车辆时:

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

您为每个实例创建原型(prototype),这有点违背了原型(prototype)继承的目的。

我肯定会选择创建对象的经典方法(使用“new”运算符):

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,则更改将仅应用于更改发生后构造的实例。
话虽如此,在这种情况下,使用“静态”成员创建对象的旧经典方法效率要高得多。

附言: 对不起,如果我被冲昏了头脑:)

关于Javascript:实例变量和静态方法;这在内存方面可以吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4548852/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com