I am attempting to follow the ES6 Builder pattern, as I am now starting to construct a more complex object.
So far I have:
class Opinion {
constructor () {
this.nuts = '';
}
}
class Builder {
constructor () {
this.opinion = new Opinion();
}
withNuts (nuts) {
this.nuts = nuts;
return this;
}
build () {
return this.opinion;
}
}
export default Builder;
Which is used:
import Builder from '../helpers/builder/opinion';
const defaultOpinion = new Builder()
.withNuts(2.9)
.build();
That outputs:
opinion: {"nuts":""}
Why is it not being built into the object?
Also, when I want to pass an object back into the Builder, to be edited, it also returns blank (which makes sense), but would my current Builder set up allow this? For example:
const opinionChange = new Builder(defaultOpinion)
.withNuts(0.8)
.build();
Many thanks for your help.