How to construct subclasses of Immutable.Record?

拟墨画扇 提交于 2019-12-19 16:59:23

问题


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

Calling new Event() seems to return a constuctor function:

new Event('started').toString()

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

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

if(!hasInitialized){ hasInitialized=true; var keys=Object.keys(defaultValues); setProps(RecordTypePrototype,keys); RecordTypePrototype.size=keys.length; RecordTypePrototype._name=name; RecordTypePrototype._keys=keys; RecordTypePrototype._defaultValues=defaultValues;}

this._map=Map(values);}"

Whereas calling the function returns the expected output:

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

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

What am I doing wrong?


回答1:


Immutable.Record "Creates a new Class which produces Record instances.", in other words it's a function in itself which you pass the allowed keys and returns a class you can extend;

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

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


来源:https://stackoverflow.com/questions/34922321/how-to-construct-subclasses-of-immutable-record

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!