0

I have a simple question, but i couldn't find the answer.

In ECMA5 javascript is there a way to method overload?

In this example I try to overload my ECMA5 class constructor, but it doesn't work.

function Classe1(arg1){
    console.log('OVERLOAD 1!');
}

function Classe1(arg1,arg2){
    console.log('OVERLOAD 2!');
}

var obj = new Classe1('arg1');
var obj = new Classe1('arg1','arg2');

When I run the code above it returns 'OVERLOAD 2' 2 times.

Caju
  • 9
  • 6
  • No, there's not. (And not in ES6 or later either). – Bergi Jan 06 '21 at 17:57
  • 1
    Thanks, I hadn't found any question similar to mine, but there it is. Thanks again! – Caju Jan 06 '21 at 17:59
  • While technically overloading is not in JavaScript, because all arguments are optional in JavaScript, you can make a single method with all possible arguments and then within that function test to see which were passed. In that way, you can simulate overloading. – Scott Marcus Jan 06 '21 at 18:02
  • Yes, that's exactly what i did! – Caju Jan 06 '21 at 20:43

0 Answers0