Is it possible to create custom operators in JavaScript?

后端 未结 9 1922
暖寄归人
暖寄归人 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:15

    No. You can't do that in JS.

    The closest you can have IMO is to implement your own object which has a chainable interface, aka "fluent" syntax. That way you can operate as if you were speaking out in sequence.

    var eq = new YourEquationObject();
    
    // Then operate like 1 - 2 * 3
    eq.add(1).sub(2).mul(3);
    

    Details are up to you though. Just giving out an idea.

提交回复
热议问题