add
it would be for example styled like this:
class example {
constructor() {}
add <otherclassname>(input) {}
sub <otherclassname>(input) {}
mul <otherclassname>(input) {}
div <otherclassname>(input) {}
}
this sort of way to do it has already been implemented with get
and set
.
it would also be consistant with them.
If this
is used as classname for other type it will be executed on operator with same type.
this would be handy for things like a vector3
class, where instead of
Vector3.add(a, b); //One way to do it
a.add(b); //Another way to do it
we can just do
a += b //Option 1
a = a + b //Option 2
This would make lots of code much more intuitive and understandable, not to forget that it would also make javascript much better of an option for game development, and make the language better overall.
Some notes on implementation:
If an invalid type is used, <operator> null
will be ran with as input the value, if this doesn't exist, it will throw a TypeError
.
If a class of that type for it does not exist, and is not defined within the same script before ending it if it is a module, an error is thrown of type ReferenceError
.
By importing before the class this can be avoided.
Sime recommendations on implementation:
Do not do something like python did, with __add__
and __sub__
, this would be harder to understand and less intuitive, not to mention that it would be a bit strange, since other things like contructor don't do this.