如何构造Immutable.Record的子类?

use*_*758 8 javascript ecmascript-6 immutable.js

class Event extends Immutable.Record {
  constructor(text) {
    super({text: text, timestamp: Date.now()});
  }
}
Run Code Online (Sandbox Code Playgroud)

调用new Event()似乎返回一个constuctor函数:

new Event('started').toString()
Run Code Online (Sandbox Code Playgroud)

"function Record(values){if(values instanceof RecordType){return values;}

if(!(此实例为RecordType)){return new RecordType(values);}

if(!hasInitialized){hasInitialized = true; var keys = Object.keys(defaultValues); setProps(RecordTypePrototype,密钥); RecordTypePrototype.size = keys.length; RecordTypePrototype._name =名称; RecordTypePrototype._keys =键; RecordTypePrototype._defaultValues = defaultValues;}

this._map =地图(值);}"

而调用函数会返回预期的输出:

new Event('started')().toString()
Run Code Online (Sandbox Code Playgroud)

"Record {"text":"started","timestamp":1453374580203}"

我究竟做错了什么?

Joa*_*son 15

Immutable.Record" 创建一个生成Record实例的新类.",换句话说,它是一个函数,你传递允许的键并返回一个你可以扩展的类;

class Event extends Immutable.Record({text:'', timestamp:''}) {
  constructor(text) {
    super({text: text, timestamp: Date.now()});
  }
}

> new Event('started').toString()
Event { "text": "started", "timestamp": 1453376445769 }
Run Code Online (Sandbox Code Playgroud)