Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function add (...xs) {
- const _add = (..._xs) => add(...xs.concat(_xs))
- _add[Symbol.toPrimitive] = () => xs.reduce((s, x) => s + x)
- return _add
- }
- add(1)(2) // 3
- add(1, 2, 3)(10) // 16
- add(1)(2)(3)(4)(5) // 15
- // const obj = {
- // valueOf: function () {
- // console.log('valueOf')
- // return {} // not a primitive
- // },
- // toString: function () {
- // console.log('toString')
- // return {} // not a primitive
- // },
- // [Symbol.toPrimitive]: function () {
- // console.log('toPrimitive')
- // return 0
- // }
- // }
Add Comment
Please, Sign In to add comment