什么是“变量=变量||” {}"用JavaScript表示 [英] What does "variable = variable || {}" mean in JavaScript
本文介绍了什么是“变量=变量||” {}"用JavaScript表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
变量的初始化代表什么:
What does this initialization of a variable stand for:
var variable = variable || {} ;
应该如何以及何时使用?
How and when should it be used?
推荐答案
该行代码执行以下操作:
That line of code does the following:
IF 变量
未定义(或具有假名值)那么将其设置为空对象。
IF variable
is not defined (or has a falsey value) THEN set it to an empty object.
ELSE 什么都不做(从技术上讲,变量
被分配给自己)
ELSE do nothing (technically speaking, variable
gets assigned to itself)
换句话说<$> c $ c>变量将转换为空对象,如果它是以下任何一个:
In other words variable
will be converted to an empty object if it is any of the following:
- false
- undefined
- null
- zero
- NaN
- 空字符串
- false
- undefined
- null
- zero
- NaN
- an empty string
参见 toBoolean ,用于规范 falsey 值的定义。
See toBoolean for the spec's definition of falsey values.
这篇关于什么是“变量=变量||” {}"用JavaScript表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文