Is it possible to create custom operators in JavaScript?

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

    I was asking this question too, and here's my answer:

    function myCustomOperation(string){
    var res = String.fromCharCode(9762);
    var thing = string.replace(/☢/g," + 10 + ");
    thing=thing.replace(/☣/g," * 2 + ");
    thing=thing.replace(/☠/g," / 3 * ");
    return [eval(thing),string+" = "+eval(thing)];
    };
    var thing = myCustomOperation("
    
    3 ☠ 4
    
    ");
    document.write(thing[1]);
    

提交回复
热议问题