During the Math classes we learned how to define new operators. For example:
(ℝ, ∘), x ∘ y = x + 2y
This defines ∘
law. For any real numbers x and y, x ∘ y is x + 2y.
Example: 2 ∘ 2 = 2 + 4 = 6
.
Is possible to define operators like this in JavaScript? I know that a function would do the job:
function foo (x, y) { return x + 2 * y; }
but I would like to have the following syntax:
var y = 2 ∘ 2; // returns 6
instead of this:
var y = foo(2, 2);
Which is the closest solution to this question?
valueOf
with existing operators, but that's about it in JavaScript. – Sharpeyedinfixl 6 ∘
. Second line:x ∘ y = x + 2 * y
. – Spin<script>
block. This technique is used quite widely. Some well-known examples include Google Traceur. – Jereme