如何构造 Immutable.Record 的子类

How to construct subclasses of Immutable.Record?

本文关键字:子类 Record Immutable 何构造      更新时间:2023-09-26
class Event extends Immutable.Record {
  constructor(text) {
    super({text: text, timestamp: Date.now()});
  }
}

调用new Event()似乎返回了一个构造函数:

new Event('started').toString()
"函数 Record(values){

if(values instanceof RecordType){ return 值;}

if(!(this instanceof RecordType)){ return new RecordType(values);}

if(!hasInitialized){ hasInitialized=true; var 键=对象键(默认值);setProps(RecordTypePrototype,keys); 记录类型原型大小=键长度;RecordTypePrototype._name=名称; RecordTypePrototype._keys=键; RecordTypePrototype._defaultValues=默认值;}

this._map=Map(values);}"

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

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

记录 { "文本": "开始", "时间戳": 1453374580203 }"

我做错了什么?

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 }