如何构造Immutable.Record的子类?

时间:2016-01-21 11:16:32

标签: javascript ecmascript-6 immutable.js

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

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

new Event('started').toString()
  

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

     

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

     

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

     

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

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

new Event('started')().toString()
  

“记录{”文字“:”已开始“,”时间戳“:1453374580203}”

我做错了什么?

1 个答案:

答案 0 :(得分:14)

Immutable.Record" Creates a new Class which produces Record instances。",换句话说,它是一个函数,你传递允许的键并返回一个你可以扩展的类;

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

> new Event('started').toString()
Event { "text": "started", "timestamp": 1453376445769 }