Take the tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
1  
Another name for this is infix function notation - the answer is no, you cannot –  Eric yesterday
 
You could try to exploit valueOf with existing operators, but that's about it in JavaScript. –  elclanrs yesterday
 
I would argue that it's a really bad idea to want to define an operator for which the symbol does not feature on my keyboard –  Eric yesterday
 
No. You can't do that in JavaScript. However, you can do it in Haskell. First line: infixl 6 ∘. Second line: x ∘ y = x + 2 * y. –  Aadit M Shah yesterday
add comment

6 Answers

up vote 7 down vote accepted

The short answer is no. EcmaScript (the standard JS is based on) does not support operator overloading.

The more interesting answer is not yet. Support is coming in EcmaScript 7 with value types.

Here is a slide deck by language creator and Mozilla CTO Brendan Eich about the subject.

It is possible to use third party tools like sweet.js to add custom operators though that'd require an extra compilation step.

share|improve this answer
 
Can you add a reference? How will I define such an operator after it will be implemented? –  イオニカ ビザウ yesterday
 
I doubt any version of ECMAScript comes with the ability to define new symbolic operators - that's normally reserved for languages like haskell. I suspect it'll only come with operator overloading –  Eric yesterday
 
@Eric what Haskell does is not magic. There is no trouble introducing backticks for infix notation in JS :) However yes, the current proposal discusses operator overloading. –  Benjamin Gruenbaum yesterday
 
@BenjaminGruenbaum: Sure, it's not magic, but defining symbolic operators is a slippery slope to perl –  Eric yesterday
 
I see no mention on the ECMAScript page on value objects suggesting that user code can declare new value types –  Eric yesterday
show 1 more comment

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.

share|improve this answer
add comment

No. JavaScript does not support operator overloading . but you can make a class method for doing this

var mathClass = function(value){
   this.value = value;
}

mathClass.prototype.toLaw = function(){
   return 2 * this.value;
}

var y = new mathClass(2)
2 + y.toLaw(); //2 + 2 * y
share|improve this answer
 
You can go one step further and add these to Number.prototype, as in my answer –  Eric yesterday
add comment

You can add pseudo-operators via methods on Number.prototype:

Object.defineProperty(Number.prototype, 'myOp', {
    value: function(that) {
        return this + 2 * that;
    }
});

Then all of this syntax will work

alert( (2).myOp(2) )
alert( 2 .myOp(2) )
alert( 2..myOp(2) )
alert( 2.0.myOp(2) )

2.myOp(2) does not work because the period is treated as a decimal point

share|improve this answer
add comment

The slightly longer then the short one is that Yes you can, but its a bit more involved then what you did in Math class

To extend the language with new constructs you can use a transformer like http://esprima.org/ or any of the others. You need to define your syntax, write the parser to parse your statement and finally add the actual code to do the math parts. When these parts is in place you have made a new language that works just as javascript but with the added support of the operator.

Its really not that hard to add new syntax, here is facebooks example how they add => arrow function syntax

https://github.com/facebook/jstransform/blob/master/visitors/es6-arrow-function-visitors.js

share|improve this answer
add comment

Read the comments below the answer.

Apparently you can't. Here is something close :

function exec(input) {
    return Function(
        'return ' + input.replace(/∘( *[\d.]+)/g, '+ 2 * $1') + ';'
    )();
}

exec('2 ∘ 2'); // 6
share|improve this answer
3  
(1 + 1) ∘ (1 + 1) will ruin your day –  Eric yesterday
1  
 
@Eric Good approach, but '2 ∘ 2' is a string. I would like not to be a string. –  イオニカ ビザウ yesterday
1  
@イオニカビザウ: The idea is you mark a script tag as language="myjavascript", and then have a small script to translate your code into raw javascript at runtine –  Eric yesterday
 
To do it properly, you'll want a javascript AST library like acorn or esprima. If you want to add new symbolic operators, you'll probably have to tweak the parser a little –  Eric yesterday
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.