【问题标题】:Javascript shorthand for assining a value only is variable exists仅当变量存在时才分配值的 Javascript 简写
【发布时间】:2014-03-24 11:08:01
【问题描述】:

以下内容的 JS 简写是什么:

    if (typeof bfMax !== 'undefined') {
        options.max = bfMax;
    }

【问题讨论】:

标签: javascript


【解决方案1】:

有条件地赋值的简洁、可读、可维护、简写是:

if (typeof bfMax !== 'undefined') {
    options.max = bfMax;
}

您似乎已经在使用它了。

如果您想缩小脚本以使其更短、可读性更低且不可维护,您可以使用:

typeof bfMax!=='undefined'&&(options.max=bfMax)

当然,你会想换掉你的变量名来缩短内容:

typeof b!=='undefined'&&(o.max=b)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-10
    • 2013-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-10
    • 1970-01-01
    • 2017-07-08
    相关资源
    最近更新 更多