简体   繁体   中英

Javascript shorthand for assining a value only is variable exists

What is a JS shorthand for the following:

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

The concise, readable, maintainable, shorthand for assigning a value conditionally is:

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

and it appears that you're already using it.

If you want to minify the script to make it shorter, less readable, and unmaintainable, you can use:

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

Of course, you'll want to swap out your variable names to make things shorter:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM