Last active
July 9, 2019 14:11
-
-
Save sp90/7f334a64c72a1bc37723d84023883d85 to your computer and use it in GitHub Desktop.
A simple way of chaining methods
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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