[removed] use either a variable, or if it's undefined, a default string

后端 未结 7 959
太阳男子
太阳男子 2020-12-10 00:49

I have this code:

var phrase = function (variable, defaultPhrase) {
    if (typeof variable === \"undefined\") {
        return defaultPhrase;
    }
    else         


        
7条回答
  •  萌比男神i
    2020-12-10 01:21

    You don't need a function. The || operator is usually used:

    Ext.Msg.show({ title: js_shutdown || 'Shutdown', //...
    

    You can see || as:

    someValue || defaultValue
    

    For strings, defaultValue is used if someValue === "".

    If the variable is not defined at all, you'll need to inline the typeof x === "undefined" check, because you cannot pass the variable to a function (that's a ReferenceError).

提交回复
热议问题