Init object in javascript using
This question already has an answer here:
The ||
operator returns the left operand if it evaluates as true, otherwise it evaluates and returns the right operand. In other words, a || b
a || b
is equivalent to a ? a : b
a ? a : b
except that a
is only evaluated once.
To understand the || operator, let's first look at a fairly basic example. The logical OR operator may be used to provide a default value for a defined variable as follows:
var bar = false,
foobar = 5,
foo = bar || foobar; // foo = 5
In this case, foo will only be assigned the value of foobar if bar is considered falsy. A falsy value could be considered being equal to 0, false, undefined, null, NaN or empty (eg "").
这会初始化myObj,除非它已经被定义。
链接地址: http://www.djcxy.com/p/73372.html上一篇: 空合并运算符角2
下一篇: 在JavaScript中使用初始化对象