DEV Community

Discussion on: Multiply a string? 🤔

Collapse
 
alohci profile image
Nicholas Stimpson

Are you familiar with languages that allow operator overloading? One big problem is achieving a common understanding of what an operator on a object means. For example, why is "Hello world" * 2 = ["Hello world", "Hello world"] and not "Hello worldHello world"?

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀

As a JavaScript guy, we do have OO but its not well known and a bit of a hack. Although it looks cool, it's exotic, so I will never use the technique for fear of other developers scratching thier heads... That being said a plus operator against a string does have quite a bit of terrible type coercion potentials, all very confusing. I suppose the only way to avoid this is to have specifications, OO is not in the spec for JavaScript

Collapse
 
peerreynders profile image
peerreynders • Edited
  1. JavaScript is truly object-oriented - you can create "objects" directly without the help of constructor functions or classes. JavaScript doesn't implement class-based object orientation like mainstream OOP languages (which are essentially class-oriented). JavaScript classes are a template for creating objects. (Though in some ways JavaScript is also function-oriented)

  2. Function overloading isn't an OO feature. For example Erlang (a functional language) allows the reuse of a function name as long as the arity (number of accepted arguments) varies. And in C++ you can overload non-member functions so procedural code can use overloaded functions (same name but the parameter type signature varies). Operator overloading tends to be an extension of function overloading.

Thread Thread
 
adam_cyclones profile image
Adam Crockett 🌀

OO oporator overloading

Not OOP

dev.to/adam_cyclones/oporator-over...