Is it possible to create custom operators in JavaScript?

后端 未结 9 1918
暖寄归人
暖寄归人 2020-12-03 04:20

During the Math classes we learned how to define new operators. For example:

(ℝ, ∘), x ∘ y = x + 2y

This defines law. For any r

9条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-03 05:16

    Set of compiled to JS languages support custom operators.

    I would highlight ReasonML (ocaml-syntax-readable-by-js-folks) and Bucklescript (ocaml-to-js-compiler) which makes custom operators look neat:

    For example an operator to concatenate strings can look like:

    let (>|<) = (list, seperator) => Belt.List.reduce(list, "", (a, b) => a ++ seperator ++ b);
    

    which can then be used like:

    [Styles.button, Media.Classes.atLeastTablet] >|< " "
    

    The downside of all this is the fact it has to be written in such compiled-to-js language, and it comes with lots of pros and cons, but usually those languages have the appeal of tons of nice syntactic sugar you don't get in js/ts

提交回复
热议问题