我正在用JS编写一组相关对象,这些对象要存储在PouchDB中,但是默认的原型继承行为不会影响持久层。我如何有一个结构可以继承父级的某些属性作为可枚举的OwnProperty,但可以继承其他属性(函数)(如原型)?我需要使某些属性可写/可配置,并使某些冻结,并且我将拥有10e5-10e6对象,因此牢记内存占用空间和性能至关重要。
PouchDB将接受任何JS对象并刮除其可枚举的属性,从而跳过原型属性。这很合理,PouchDB很棒。 我的对象是一个家族,它们共享一些属性和方法,每一代都增加一些属性和方法,并保留所有父代的属性和方法。
为简单起见,我将使用Shapes trope。
Shape.properties={
x:0, // Vanilla x displacement
y:0, // Vanilla y displacement
color:red, // We want this to be immutable
};
Shape.methods={
move:function(x,y){this.x+=x; this.y+=y;}, // Change displacement
reset:function(){this.x=0; this.y=0;}, // Back to origin
save:function(){db.put(this);}, // Persist to DB
}
Rectangle.properties={
w:2, // Width
h:1, // Height
};
Rectangle.methods={
aspect:function(w,h){this.w=(w/h)*this.h;}, // Stretch
};
Cube.properties={
z:0, // Elevation
};
Cube.methods={
lift:function(z){this.z+=z;}; // Float up
};
如果我使用普通的JS原型继承并创建一个多维数据集,则它将仅具有z的OwnProperties,这使持久性无用。 因此,我制作了一个帮助程序模块来解决此问题,该模块具有一些功能,可以使用位掩码和组合功能将所有零件拼凑在一起,从而快速制作PropertyDescriptor:
// writable=4 | enumerable=2 | configurable=1
/* Process a bitMask that describes the desired property descriptor. We then use the combine method to add
* specifically described properties to an object. PouchDB will grab any enumerable properties, some should be write-protected
* but vanilla properties are Accessors with bitmask 6(enumerable and writable). Based on code from elsewhere. */
accessor:function(bMask,val){ // Accessor descriptor: bitmask , value
return {configurable:Boolean(bMask & 1),enumerable:Boolean(bMask & 2),writable:Boolean(bMask & 4),value:val};
},
combine:function(root,properties,methods){ // This is a naive implementation, ask SO for help?
for (let i in root){ // Iterate over properties.
properties[i]=Object.getOwnPropertyDescriptor(root,i); // Combine the root properties and the given properties objects,
}
methods=Object.defineProperties(Object.getPrototypeOf(root),methods);// Add the methods to the root prototype.
return Object.create(methods,properties); // Combine the prototype and the properties
},
我的结构现在看起来像
Shape.properties={
x:accessor(6,0),
y:accessor(6,0),
color:accesor(2,red), // Bitmask 2: enumerable only
};
Shape.methods={
move:accessor(0,function(x,y){this.x+=x; this.y+=y;}), // Bitmask 0: 'Frozen' property
reset:accessor(0,function(){this.x=0; this.y=0;}),
save:accessor(0,function(){db.put(this);}),
}
var Shape=function(){
return combine(new Object(),Shape.properties, Shape.methods);
};
Rectangle.properties={
w:accessor(6,0),
h:accessor(6,0),
};
Rectangle.methods={
aspect:accessor(0,function(w,h){this.w=(w/h)*this.h;}),
};
var Rectangle=function(){
return combine(new Shape(),Rectangle.properties, Rectangle.methods);
};
//...
因此,这可行,但是两个相同类型的对象不再共享原型,也就是说,每个对象都有一个唯一的原型实例。
这是对内存的废话,通常是不好的做法。我不妨将这些方法写到每个对象上,然后让小袋将它们作为错误吐给我。
我研究了其他选项并遇到了this article,这有一些很好的线索,但是后来我读了this,这让我觉得也许我应该创建静态/冻结原型对象并从那里构建或完全采用另一种方法。
我也看过使用Object.assign(),但是它可以大量修改要复制的属性。
很可能有一个我没有看到的简单解决方案,您知道它是什么吗?还是可以帮我找到它?
答案 0 :(得分:0)
因此,环顾四周并进行了更多实验后,我发现冻结的原型似乎可以正常工作,并假设它不会触发上述优化错误。我现在的结构就像
const methods={},properties={};
properties.Shape=Object.freeze({
x:{value: 0, writable: true, enumerable: true, configurable: true},
y:{value: 0, writable: true, enumerable: true, configurable: true},
color:{value: 'red', writable: false, enumerable: true, configurable: false},
});
methods.Shape=Object.freeze({
move:{value:function(x, y) {
this.x += x;
this.y += y;
console.info('Shape moved.');
return this;},writable: true, enumerable: true, configurable: true},
});
const Shape=function() {return Object.defineProperties(this,properties.Shape);}
Shape.prototype=Object.freeze(Object.defineProperties({},methods.Shape));
properties.Rectangle=Object.freeze({
w:{value: 0, writable: true, enumerable: true, configurable: true},
h:{value: 0, writable: true, enumerable: true, configurable: true},
});
methods.Rectangle=Object.freeze({
zoom:{value:function(z){
this.w=this.w*z;
this.h=this.h*z;
console.info('Rectangle zoomed');
return this;},writable: true, enumerable: true, configurable: true},
});
const Rectangle=function() { Shape.call(this); return Object.defineProperties(this,properties.Rectangle);};
Rectangle.prototype = Object.freeze(Object.create(Shape.prototype,methods.Rectangle));
var rect = new Rectangle();
console.log('Is rect an instance of Rectangle?',
rect instanceof Rectangle); // true
console.log('Is rect an instance of Shape?',
rect instanceof Shape); // true
rect.move(1, 1); // Outputs, 'Shape moved.'
rect.zoom(2); // {x:1,y:1,w:0,h:0}
rect2=new Rectangle();
console.log('Do rect2 and rect share a prototype?',
rect2.prototype===rect.prototype); // yes
shape=new Shape();
shape.move(2,2); // moves
shape.zoom(2); // Fails as expected
使用助手功能对其进行清理应该使其更漂亮且更易于更新。