从 Array 和另一个类扩展的 Javascript 类

标签 javascript arrays ecmascript-6 es6-class

如何制作或模拟扩展另一个类和 Array 的 javascript 类。我有另一个类 Serialized,它还需要扩展以具有 serializedeserialize 函数。

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 的方法 serializedeserialize ,同时还扩展 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/

相关文章:

javascript - createElement 方法可以应用于除文档之外的任何节点吗?

c++ - 没有新运算符的动态数组?

javascript - 模板文字 : how to perform substitution correctly?

node.js - Azure 函数的 Node 和 DirectlineJS es6 导出的版本兼容性问题

javascript - jQuery 重置 newsticker 的先前效果而不是通过单击替换为新效果

javascript - jQuery 对话框不调整 div 中内容的大小

javascript - Jquery - IE 未实现错误(在 IE 8 中)

ruby - 填充稀疏数组

android - 如何使用 JSON Web 服务填充 Android 微调器?

javascript - 使用类验证器中的 @MinDate 会导致 TypeError : Cannot read property 'getTime' of undefined?