13

I am learning Javascript now and coming from the background of Java. In Java we have overloaded constructors wherein, at compile time, the compiler will decide which version of constructor to be called.

In case of Javascript, we can have only one constructor. In this case, if the constructor has 3 arguments, how to pass only the third parameter.

Ex:

class Addition {
 constructor(a,b,c){
   console.log(a+B+c);
 }
}

Addition obj = new Addition(null, 40, null);
//or
Addition onj2 = new Addition(undefined, undefined, 45);

Is there a better way to do this in Javascript?

zilcuanu
  • 3,451
  • 8
  • 52
  • 105
  • 1
    There is no notion of overloading in javascript. You can still use a factory though – Vivick Jun 06 '18 at 07:57
  • 1
    I would prefer a builder over 3 three constructors, even more so if the parameters have the same type. Or you use different factories as suggested by @Vivick. Overloading isn't exactly a recommended pattern as it can lead to guess work. – a better oliver Jun 06 '18 at 10:56

3 Answers3

21

You may use EcmaScript's parameter destructuring with default values to achieve that. Additionally, you need to use const (or let, or var) keyword to create variables.

class Addition {
  constructor({a = 0, b = 0, c = 0}) {
    console.log(a + b + c);
  }
}

const obj = new Addition({a: null, b: 40, c: null});
const onj2 = new Addition({c: 45});
31piy
  • 23,323
  • 6
  • 47
  • 67
  • 1
    What is the difference between `constructor(a=null, b=null,c=null) and constructor({a=null,b=null,c=null})`? – zilcuanu Jun 06 '18 at 08:34
  • 3
    @zilcuanu -- With former, you cannot pass the value for `c` only, but with latter, you can. – 31piy Jun 06 '18 at 08:36
  • JavaScript has no named parameters. The OP's example has three parameters, yours only one. It also does not solve the problem of selecting a different function (or any piece of code) based on the number of arguments. – a better oliver Jun 06 '18 at 10:30
  • 1
    @abetteroliver -- I've re-phrased my answer. I believe it addresses the first point of your comment. My answer essentially targets OP's this statement: _how to pass only the third parameter_, as it was already pointed out in comments that JavaScript doesn't support function overloading. – 31piy Jun 06 '18 at 10:42
2

There is no possibility of overloading in JS, but you can work with Object destructuring. They can even be complex and refer to other default parameters, like:

function rangeMessage({from=1, to=from+1, message=(counter) => `Hello ${counter}`}={}) {
  for(from; from <= to; from++) {
    console.log(message(from));
  }
}

rangeMessage({from: 10, to: 20});
rangeMessage({to: 10, message:(counter) => `Bye ${counter}`})
rangeMessage()

function sumABC({a=null, b=null, c=null}={}) {
  let result = 0;
  if(a) result += a;
  if(b) result += b;
  if(c) result += c;
  return result;
}

console.log(sumABC());
console.log(sumABC({a:10}));
console.log(sumABC({b:5}));
console.log(sumABC({b: 10, c: 5}));

The {} = {} part does the same as the other destructuring assignments - if no object was passed in, assume an empty object (which is then filled by the object destructuring statements from the left side).

wiesion
  • 2,349
  • 12
  • 21
1

I hope this solved you question :D

class Addition {
 constructor(a=null,b=null,c=null){
   console.log(a+b+c);
 }
}

var obj = new Addition(null, 40, null);
var onj2 = new Addition(undefined, undefined, 45);
Akhil Aravind
  • 5,741
  • 16
  • 35