Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to construct subclasses of Immutable.Record?

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?

like image 801
user3452758 Avatar asked Jan 21 '16 11:01

user3452758


1 Answers

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 }
like image 183
Joachim Isaksson Avatar answered Oct 05 '22 23:10

Joachim Isaksson