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.
精彩评论