如何制作或模拟扩展另一个类和数组的javascript类。我还有另一个类Serializable
,它也需要扩展以具有serialize
anddeserialize
函数。
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 */
}
使用这个我有一个可序列化的类。我如何才能使它具有方法serialize
并deserialize
从Serializable扩展Array
我会按照可能的重复说明中的建议使用mixins,但是如何使用mixins具有静态功能 deserialize
在JavaScript中,类是表达式。您可以利用此优势:
function addSerialize(superclass) {
return class Serialize extends superclass {
// serialize class goes here
}
}
class MyArray extends addSerialize(Array) {
// MyArray stuff goes here
}
AFAIK,Google的Justin Fagani提出了这种方法。他还编写了一个小帮助程序库来帮助实现它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句