Skip to main content

News

  • The forums have now switched to ElkArte, an SMF fork which is more modern and maintained better!

Topic: Operator Overloading in JS (Read 1069 times) previous topic - next topic

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Operator Overloading in JS
Guys, here's something neat I think some of you might find interesting. I was thinking of how I can overload operators in JS and was thinking about using the toString method, but if you use valueOf instead you can add some interesting logic to make operator overloads possible.

Pay attention to how the number 3 is used to figure out which operator was requested in the overload process.

Article: http://www.2ality.com/2011/12/fake-operator-overloading.html
GitHub Demo: https://github.com/rauschma/op_overload
If you use code to help you code you can use less code to code. Also, I have approximate knowledge of many things.

Sphere-sfml here
Sphere Studio editor here

  • Fat Cerberus
  • [*][*][*][*][*]
  • Global Moderator
  • miniSphere Developer
Re: Operator Overloading in JS
Reply #1
This is a pretty neat trick, although I dislike operator overloading in general because it tends to be a very leaky abstraction.  For example, you can't do "abc" + "def" in C++, you have to cast at least one of the literals to a std::string.
miniSphere 4.8.4 - Cell compiler - SSj debugger
Forum Thread | GitHub Repo

  • Flying Jester
  • [*]
  • Verified
  • TurboSphere Developer
Re: Operator Overloading in JS
Reply #2
Operator overloading is also very horrifically abused in C++. The io system in the C++ standard library is one of the worst examples of using operator overloading well, which is very unfortunate since it's most folks introduction to operator overloading in C++.

Nowadays I tend to just write my own io wrapper around C's io streams, using variable templates. It's just nicer to do in every way than C++'s standard library io.

One thing that has been long overdue is the new literal notation, which in C++14 will let you define a string literal as a std::string. Then you could do it without a cast. I like this compromise, since it means a string is just an array of chars normally, but can optionally be a std::string, if that's what you need. It's probably the only part of C++14 (or is it C++17?) I am excited for.

  • Fat Cerberus
  • [*][*][*][*][*]
  • Global Moderator
  • miniSphere Developer
Re: Operator Overloading in JS
Reply #3
I guess the standards committee was trying to keep the language and standard library decoupled.  But that kind of thing is exactly what makes C# such a joy to work with--much of what it offers is just compiler magic around some feature or other of the .NET framework.  For example a 'using' block is just sugar for calling Dispose at the end (which is valuable--it saves a finally clause), and async/await is a very clean abstraction of threads and promises (or Tasks as .NET calls them).  In C++ the STL is completely auxiliary and feels at times more like a warty appendage than a proper compliment to the language.
miniSphere 4.8.4 - Cell compiler - SSj debugger
Forum Thread | GitHub Repo