Home > Blockchain >  How to set default value for Function object?
How to set default value for Function object?

Time:09-30

Is there a better way to add a default value to a Function object?

 function CountOfFruit(bowl){
    this.strawberries = bowl.strawberries
    this.blueberries = bowl.blueberries
    
   if (bowl.strawberries == ""){
     this.strawberries = "there is none"
   }
 }

CodePudding user response:

Yes, you can write something like:

function CountOfFruit(bowl){
    this.strawberries = bowl.strawberries || "there is none";
    this.blueberries = bowl.blueberries || "there is none";
}

In that way, if one of the attributes is null or an empty string, the default value will be used.

  • Related