Skip to content

Instantly share code, notes, and snippets.

@sp90
Last active July 9, 2019 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sp90/7f334a64c72a1bc37723d84023883d85 to your computer and use it in GitHub Desktop.
Save sp90/7f334a64c72a1bc37723d84023883d85 to your computer and use it in GitHub Desktop.
A simple way of chaining methods
class klass {
constructor() {}
sum(...args) {
this.value = args.reduce((sum, current) => sum + current, 0);
return this;
}
add(value) {
this.value = this.value + value;
return this;
}
subtract(value) {
this.value = this.value - value;
return this;
}
pipe(method) {
this.value = method(this.value);
return this;
}
log() {
console.log('klass.value: ', this.value);
return this;
}
}
new klass()
.sum(1, 2, 3) // 6
.add(10) // 16
.pipe(val => {
return val + 1;
}) // 17
.subtract(5)
.log(); // 12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment