Given an ES6 class as follows, what is the best way to keep a reference to the data passed into the constructor so it is available from instance methods?
The best I've found so far is to manually add a reference to each one to some instance property within the constructor. Is there a cleaner/easier way?
class Test {
constructor( {
option1 = 1,
option2 = 2
} = {} ) {
// What's the best way to keep a reference to
// the options on the instance?
this.options = {
option1,
option2
};
}
addOptions() {
return this.options.option1 + this.options.option2;
}
}
let t = new Test({
option1: 5
});
console.log(t.addOptions()); // 7
Here's a link to the above code running via 6to5.