class Event extends Immutable.Record {
constructor(text) {
super({text: text, timestamp: Date.now()});
}
}
调用 new Event()
似乎返回一个构造函数:
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);}"
而调用该函数会返回预期的输出:
new Event('started')().toString()
"Record { "text": "started", "timestamp": 1453374580203 }"
我做错了什么?
最佳答案
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 }
关于javascript - 如何构造 Immutable.Record 的子类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34922321/