3 ms·
Basically this is a very limited case of operator overloading. Redefine `==` so that it means value equality and not reference equality. But it would be so muc
by captainmuon 4y ago
Basically this is a very limited case of operator overloading. Redefine `==` so that it means value equality and not reference equality.
But it would be so much nicer if JavaScript had proper operator overloading. You could actually define it nicely on top of TypeScript since you usually know the types of the operands, and could rewrite the call at compile time. Unfortunately, the TS developers are philosophically opposed to adding such a feature. And I'm sceptical JS proper will add operator overloading soon.
It would be incredible for e.g. graphics programming to have proper matrix and vector types and to be able to write formulas in a natural way!
- wruza 4y agoI wanted that too until I realized they are usually used only in a couple of files in a project, only a handful of times in each. Operator overloading looks nice but it isn’t a life changer. What javascript lacks is a culture of [in]formal protocols which could provide additional semantics. It has some: toString, toJSON, @@iterator, etc. Maybe this is a good thing.
- fbn79 4y agoYou can ovveride valueOf and do something like +p1 === +p2 Check this generic implementation https://jsfiddle.net/tas2n59L/ https://jsfiddle.net/tas2n59L/