This is definitely not infix notation, but it is close to : /
let plus = function(a,b){return a+b}; let a = 3; let b = 5; let c = a._(plus).b
I don’t think that anyone really would like to use this “notation”, as it’s pretty ugly, but I think that maybe there are some settings that can be made to make them look different or better (maybe using this answer here to “call the function” without parentheses).
Infix function
// Add to prototype so that it always there for you Object.prototype._ = function(binaryOperator){ // The first operand is captured in the this keyword let operand1 = this; // Use a proxy to capture the second operand with "get" // Note that the first operand and the applied function // are stored in the get function closure, since operand2 // is just a string, for eval(operand2) to be in scope, // the value for operand2 must be defined globally return new Proxy({},{ get: function(obj, operand2){ return binaryOperator(operand1, eval(operand2)) } }) }
Also note that the second operand is passed as a string and evaluated using eval to get its value. Because of this, I think that the code will burst at any time when the value of the operand (aka “b”) is not defined globally.
Steve ladavich
source share