Guest User

Untitled

a guest
Nov 21st, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. function add (...xs) {
  2. const _add = (..._xs) => add(...xs.concat(_xs))
  3. _add[Symbol.toPrimitive] = () => xs.reduce((s, x) => s + x)
  4.  
  5. return _add
  6. }
  7.  
  8. add(1)(2) // 3
  9. add(1, 2, 3)(10) // 16
  10. add(1)(2)(3)(4)(5) // 15
  11.  
  12. // const obj = {
  13. // valueOf: function () {
  14. // console.log('valueOf')
  15. // return {} // not a primitive
  16. // },
  17. // toString: function () {
  18. // console.log('toString')
  19. // return {} // not a primitive
  20. // },
  21. // [Symbol.toPrimitive]: function () {
  22. // console.log('toPrimitive')
  23. // return 0
  24. // }
  25. // }
Add Comment
Please, Sign In to add comment