What does "options = options || {}" mean in Javascript? [duplicate]

Martin.ptrn提出了一个问题:What does “options = options || {}” mean in Javascript? [duplicate],或许与您遇到的问题类似。

回答者user2864740CMS给出了该问题的处理方式:

This is useful to setting default values to function arguments, e.g.:

function test (options) {
  options = options || {};
}

If you call test without arguments, options will be initialized with an empty object.

The Logical OR || operator will return its second operand if the first one is falsy.

Falsy values are: 0, null, undefined, the empty string (""), NaN, and of course false.

希望本文对你有帮助,欢迎支持JavaScript中文网