I am trying to build document indexes in javascript, and am having trouble figuring out the correct way to do work in a es6 constructor.
- If I dont call buildIndex, the object is not usable, so it seems like a good candidate for the constructor
- If I call super first it builds the index without the filter - so its not the correct index, or I need to throw away the old index
- If I set the this.filter first it throws an error that I haven't called super yet.
The only solution I can figure is make users call buildIndex
explicitly after construction - which seems counter intuitive and incorrect as if I need to call "construct" after construction.
Am I missing something or are ES6 constructors limited?
class TokenIndex {
constructor(document, stemmer) {
this.document = document;
this.stemmer = stemmer || (x => x);
this._buildIndex();
}
_buildIndex(){
// do expensive index build
}
}
class FilteredTokenIndex extends TokenIndex {
constructor(document, stemmer, filter) {
this.filterRegex = filter;
// Fails because super must be called before `this`
super(document, stemmer);
}
_buildIndex(){
// do expensive index build
}
}
class FilteredTokenIndex2 extends TokenIndex {
constructor(document, stemmer, filter) {
// Fails because builds the index without a filter
super(document, stemmer);
this.filterRegex = filter;
}
_buildIndex(){
// do expensive index build
}
}