How to set default boolean values in JavaScript?

后端 未结 5 1572
执念已碎
执念已碎 2021-01-31 01:38

Setting default optional values in JavaScript is usually done via the || character

var Car = function(color) {
  this.color = color || \'blue\';
};
         


        
5条回答
  •  萌比男神i
    2021-01-31 02:12

    How about:

    this.hasWheels = (typeof hasWheels !== 'undefined') ? hasWheels : true;
    

    Your other option is:

    this.hasWheels = arguments.length > 0 ? hasWheels : true;
    

提交回复
热议问题