0

I am attempting to create a class in javascript to immitate the defined schema of a java object as opposed to a chaotic/schemaless JSON object. Is it okay to use OR (||) as I did to initialize values if they are missing in the initiation of an instance. For example,

 class PlotObj {

    constructor(startHour, startMin, endHour, endMin) {
        this.startHour =startHour || 0;
        this.startMin =startMin || 0;

        this.endHour =endHour || 23;
        this.endMin =endMin || 54;
    }        
}

let obj = new PlotObj(null, null, 5, 40)

Or is there a better way to acheive this. I know you can initialize them above the constructor in the class, but I want to be able to pass parameters from any of the positions, like the example shown, where I only pass the 3rd and 4th. I would also like to, of course, be able to send the 1st and 2nd, or 1st and 3rd, and so on, any combination. Thanks!

Michael
  • 41,989
  • 11
  • 82
  • 128
Young Scooter
  • 474
  • 5
  • 14
  • I dont know about javascript, but in plain java you can just add a default constructor, with no function parameters that assigns your default values to the classes / objects variables. – GitPhilter Sep 25 '19 at 21:33
  • The most extensible way I've seen of doing this is by passing in a single associative array, and then merging the passed in array with a "default" array you have defined in the constructor. That way you can pass in only the variables you want by only defining those in the object initialization and you don't need to worry about passing in `null`s also. Though I don't think that'd work in JS where associative arrays aren't allowed. – WOUNDEDStevenJones Sep 25 '19 at 21:36
  • The question was tagged with "java" and "javascript" and i was looking for questions concerning java. after my comment OP edited his question and removed the java tag, so...but you are right. should I delete my initial comment or leave it as it is? I am new here... – GitPhilter Sep 25 '19 at 21:38
  • @GitPhilter Oh, I didn't see that the tag had been removed. (Not by the OP himself, btw) – Bergi Sep 25 '19 at 21:38
  • 1
    Yes, this is totally okay and idiomatic. You just have to care about falsy values - passing `0` as a value for the `endHour` won't work, it would become `23`. Test explicitly `endHour == null ? 23 : endHour` or `typeof endHour == "number" ? endHour : 23` or something like that. – Bergi Sep 25 '19 at 21:40
  • 1
    Also, the more modern approach is using [default initialisers for the parameters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters) - which don't work with `null` though, only with `undefined`. – Bergi Sep 25 '19 at 21:41
  • @GitPhilter I didn't change it that was somebody else. I assume the default constructor is only ran if i define the object with no parameters such as new plotObj()? – Young Scooter Sep 25 '19 at 21:42
  • @WOUNDEDStevenJones this is a good idea, but seems like a bit more work. Is there anything wrong with what I did using the ORs? – Young Scooter Sep 25 '19 at 21:42
  • @Young Scooter I have never worked with JavaScript, but a quick research tought me that you cannot overload functions in JavaScript, so I can give you no help, sorry. – GitPhilter Sep 25 '19 at 21:45
  • @YoungScooter _Wrong?_ No - as long as you've read the duplicate post talking about the pro/cons. And idk if this is possible in javascript, but https://www.grzegorowski.com/php-default-values-with-array_merge shows the example of what I was describing. The benefit of this approach is that you don't need to pass `null`s for unknown parameters. But realistically I'm not sure if there's a difference between what you're doing and just defining defaults in the arguments anyway (if you set the defaults to be `null`). Either way you're having to pass in `null` for unknown values. – WOUNDEDStevenJones Sep 25 '19 at 21:47
  • @Bergi thank you! Both of your points are awesome. I do need to watch out for 0s, great point. And also the default initializers seem very useful for this exact case, thanks. – Young Scooter Sep 25 '19 at 21:48
  • @WOUNDEDStevenJones thanks I get what you're saying – Young Scooter Sep 25 '19 at 21:50

0 Answers0