如何构造Immutable.Record的子类?
How to construct subclasses of Immutable.Record?
class Event extends Immutable.Record {
constructor(text) {
super({text: text, timestamp: Date.now()});
}
}
调用 new Event()
似乎 return 一个构造函数:
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);}"
而调用函数 return 的预期输出是:
new Event('started')().toString()
"Record { "text": "started", "timestamp": 1453374580203 }"
我做错了什么?
Immutable.Record “Creates a new Class which produces Record instances.”,换句话说,它本身就是一个函数,您可以传递允许的键,并且 returns 可以扩展 class;
class Event extends Immutable.Record({text:'', timestamp:''}) {
constructor(text) {
super({text: text, timestamp: Date.now()});
}
}
> new Event('started').toString()
Event { "text": "started", "timestamp": 1453376445769 }
class Event extends Immutable.Record {
constructor(text) {
super({text: text, timestamp: Date.now()});
}
}
调用 new Event()
似乎 return 一个构造函数:
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);}"
而调用函数 return 的预期输出是:
new Event('started')().toString()
"Record { "text": "started", "timestamp": 1453374580203 }"
我做错了什么?
Immutable.Record “Creates a new Class which produces Record instances.”,换句话说,它本身就是一个函数,您可以传递允许的键,并且 returns 可以扩展 class;
class Event extends Immutable.Record({text:'', timestamp:''}) {
constructor(text) {
super({text: text, timestamp: Date.now()});
}
}
> new Event('started').toString()
Event { "text": "started", "timestamp": 1453376445769 }