开发者

what does "window.gapi = window.gapi || {};" mean?

开发者 https://www.devze.com 2023-03-30 17:45 出处:网络
I\'m a little confused w开发者_运维技巧ith the syntax here. window.foo = window.bar || {}; Any ideas? I\'m just trying to understand javaScript better. ThanksIf window.bar is null or undefined (als

I'm a little confused w开发者_运维技巧ith the syntax here.

window.foo = window.bar || {};

Any ideas? I'm just trying to understand javaScript better. Thanks


If window.bar is null or undefined (also: 0, "", NaN and false) then window.foo will be set to an empty object ({}), otherwise it will be window.bar.

The logical OR operator (||) works as a null coalescing operator in this situation. It's basically shorthand for:

window.foo = (window.bar != null ? window.bar : {});

This post explains the behavior in more detail.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号