Skip to content

Instantly share code, notes, and snippets.

@YavorK
Last active June 1, 2023 17:27
Show Gist options
  • Save YavorK/1f901511bfcae157cf4876186c02c0b1 to your computer and use it in GitHub Desktop.
Save YavorK/1f901511bfcae157cf4876186c02c0b1 to your computer and use it in GitHub Desktop.
how to get chained methods (builder pattern :3 ) in ES6
class math {
constructor() {
this.current = 0;
}
add(number) {
this.current += number;
return this;
}
multiply(number) {
this.current *= 5;
return this;
}
getCurrent() {
return this.current;
}
}
let funMath = new math;
console.log(funMath.getCurrent()); //0
funMath
.add(5)
.multiply(5);
console.log(funMath.getCurrent()); //25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment