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?
Custom operators are also known as advanced operators and allow you to combine two instances with a self-chosen infix, prefix, postfix, or assignment operator.
What exactly is the JavaScript in operator? The JavaScript in operator is used to check if a specified property exists in an object or in its inherited properties (in other words, its prototype chain). The in operator returns true if the specified property exists.
JavaScript operators are used to assign values, compare values, perform arithmetic operations, and more.
You can make a JavaScript object in four different ways: Let’s see them one by one below. 1. Object Literals Defining an object literal is the simplest way to create a JavaScript object. As objects are variables, you can instantiate them the same way as a variable.
Logic: create a custom object directly by a much shorter and clean syntax. Logic: define a constructor first. Then, call constructor with “new” keyword to create custom object. Logic: first, define a class including a constructor. And then, call wrapped constructor by class name to create custom object.
JavaScript’s special operators are a hodge-podge of miscellaneous other symbols and words that perform other and important functions. The conditional operator (also known as the ternary operator) uses three operands. It evaluates a logical expression and then returns a value based on whether that expression is true or false.
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
The short answer is no. ECMAScript (the standard JS is based on) does not support operator overloading.
As an aside, in ECMAScript 7, you'll be able to overload a subset of the standard operators when designing custom value types. Here is a slide deck by language creator and Mozilla CTO Brendan Eich about the subject. This won't allow arbitary operators, however, and the overloaded meaning will only be applied to value types. <- haha that ended up not happening.
It is possible to use third party tools like sweet.js to add custom operators though that'd require an extra compilation step.
I've answered with a solution from outside JavaScript using esprima - this is changing JavaScript and extending it, it's not native.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With