我正在编写一个JS中我想要存储在PouchDB中的相关对象的集合,但默认的原型继承行为不会与持久层一起使用。我怎么能有一个结构可以从父类继承一些属性作为可枚举的OwnProperties但其他属性(函数)如原型?我需要使一些属性可写/可配置,一些冻结,我将有10e5-10e6对象,因此记住内存占用和性能是至关重要的。
PouchDB将接受任何JS对象并刮掉其可枚举属性,跳过原型属性。这非常有意义,PouchDB非常棒。我的对象是一个家庭,它与每一代共享一些属性和方法,每个都添加一些属性和方法,并保留所有父级。
为简单起见,我将使用Shapes转义。
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原型继承并制作一个Cube,它只会有z的OwnProperties,这使得持久性无用。所以我制作了一个帮助模块来解决这个问题,它有一些功能,我可以用它来制作快速的PropertyDescriptors,使用位掩码和组合功能来拼凑所有部分:
// 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(),但它可以大量修改被复制的属性。
可能有一个我没有看到的简单解决方案,你知道它是什么吗?或者你可以帮我找到它吗?
因此,在环顾四周并进行实验后,我发现冻结的原型似乎有效,假设不会触发上面提到的优化错误。我现在的结构就像
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
使用辅助函数清理它应该使它更漂亮,更容易更新。