如何制作或模拟扩展另一个类和 Array 的 javascript 类。我有另一个类 Serialized
,它还需要扩展以具有 serialize
和 deserialize
函数。
class ArrayLikeClass extends Serializable({"_title": "raw"}){
constructor(title){
this._title = title;
}
/* Serializable adds these:
serialize(){...serializes the class}
static deserialize(){...deserializes the class}
// I don't want to add these in manually */
}
使用这个我有一个可序列化的类。我怎样才能使其具有 Serialized 的方法 serialize
和 deserialize
,同时还扩展 Array
我会按照可能的重复中的建议使用mixins,但是如何使用mixins来拥有静态函数反序列化
最佳答案
在 JavaScript 中,类是表达式。您可以利用这一点:
function addSerialize(superclass) {
return class Serialize extends superclass {
// serialize class goes here
}
}
class MyArray extends addSerialize(Array) {
// MyArray stuff goes here
}
据我所知,Google 的 Justin Fagani 提出了这种方法。他还写道a little helper library协助实现。
关于从 Array 和另一个类扩展的 Javascript 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50106390/