问题
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