If height param is not number - then log and set to default I am creating a class with a constructor and instead of double handling, I thought I would use a one line conditional and if the class property is not set correctly then console.log out a message.

I want to know if there is a cleaner implementation for the following height property as at the moment I don't think readability is overly terrific.

This is basically what I have:



class Example1 {
  constructor(height) {
    this.defaultHeight = 100;

    this.height = !isNaN(height) ? height : (() =&gt; {
      console.log("That was not a number &gt;&gt; " + height);
      return this.defaultHeight
    })();

  }
}

//////////////////////////////////////////////////////////////////////
console.log("init example 1 w/ height of 0")
var example1 = new Example1(0);
console.log("example1.height is ", example1.height);
console.log("init example 2 w/ height of 'bob'")
var example2 = new Example1("bob");
console.log("example2.height is ", example2.height);
I usually only use ternaries if I can keep my code clean and at the same time keep the ternary under 2-3 lines. That's just me, but I would make a few small changes to increase readability.

class Example1 {
  constructor(height) {
    this.defaultHeight = 100;

    this.height = typeof height === 'number' ? height : 
      (() =&gt; {
        console.log(height, 'is not a number');
        return this.defaultHeight;
      })();

  }
}