Skip to content

Instantly share code, notes, and snippets.

@NullDev
Last active September 5, 2020 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NullDev/5b7515558ed044f35eb3e4f6515a2026 to your computer and use it in GitHub Desktop.
Save NullDev/5b7515558ed044f35eb3e4f6515a2026 to your computer and use it in GitHub Desktop.
A JavaScript implementation to allow expressions such as 3 > 2 > 1
/**
* This basically allows expressions like 3 > 2 > 1
* which is usually written as 3 > 2 && 2 > 1
*
* I basically did this because I wanted to see how easy it is
* to implement such "new syntax" in JS
*
* Please don't use this. It's hacky.
*/
let lastRightHandOperator = null;
Number.prototype[">"] = Boolean.prototype[">"] = function(x){
if (isNaN(parseFloat(this))){
let cache = lastRightHandOperator;
lastRightHandOperator = this;
return (this && cache > x);
}
lastRightHandOperator = this;
return this > x;
};
console.log(
8 [">"] (5) [">"] (3)
);
// true
console.log(
3 [">"] (5) [">"] (3)
);
// false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment