0

I have seen creating the base class as following way in one of the Node.js project. I can't understand why the datatype is used next to the pipe operator. I'm not sure if this is the right way to create properties for a class.

I am sure this.name = name || "" is the right way. I just want to know if this.name = name || String; is also right way to create properties of class.

class MeetingBase {
    constructor(name, startdate, enddate, active) {
        this.name = name || String;
        this.startdate = startdate || Date;
        this.enddate = enddate || Date;
        this.active = active || Boolean;
    }

}
Prem
  • 5,685
  • 15
  • 52
  • 95
  • 3
    No, that's definitely not right. Looks like it was written by someone who doesn't know what they're doing. Silly question, but you're sure that it was an or-operator (`||`) and not a comment (`// String`), right? – JLRishe Jul 07 '18 at 15:58
  • 2
    *one of the Node.js project* - what project? It doesn't much sense without knowing what happens with this class. – Estus Flask Jul 07 '18 at 15:59
  • I tested it and `this.name = name || ""` is right, tho you could try `this.name = name || new String()` – Michael Jul 07 '18 at 16:00
  • @GetOffMyLawn how? – Michael Jul 07 '18 at 16:02
  • 1
    It looks weird - just using `String` like that will assign the `String` function to the member (if the arg is unset, of course). I'm really interested from which project is it and what does it mean. – arieljannai Jul 07 '18 at 16:05

0 Answers0