对象数组的 JavaScript 字面表示法
我试图理解一些用 Javascript 编写的代码,但我被语法困住了。
var connections = {}; // I know '{}' means new Object()
var messages = {};
对象包含变量路径、版本等,但是在代码中以下几行做了什么?
connections[ path ] = connections[ path ] || [];
messages[ path ] = messages[ path ] || { version: 0, body: ''};
I'm trying to understand some code written in Javascript and I'm stuck rather the syntax.
var connections = {}; // I know '{}' means new Object()
var messages = {};
the objects contains variables path, version etc. but then in the code what do the following lines do?
connections[ path ] = connections[ path ] || [];
messages[ path ] = messages[ path ] || { version: 0, body: ''};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
由于
connections
和messages
是对象,因此[
和]
并不表示数组索引,而是表示语法访问成员,其中使用变量来查找变量的名称。上面,由于
path = 'x'
等于 ,也就是说,如果
path
的值命名的两个对象的成员存在,则保留它(分配其当前值本身),否则(||
用于此处合并)分别创建一个新的空数组或一个新的{ version: 0, body: '' }
。请注意,合并/逻辑或可以轻松地从左到右链接。例如,在某些情况下,您可能想做这样的事情:
Since
connections
andmessages
are objects, the[
and]
does not denote array indices, but is rather the syntax to access members, where a variable is used to find the name of the variable.The above, since
path = 'x'
is equal toIt's saying, if the member of the two objects named by the value of
path
exists, keep it (assign its current value to itself), otherwise (||
is used for coalesce here) create a new empty array, or a new{ version: 0, body: '' }
, respectively.Note that the coalesce / logical OR can easily be chained from left to right. You might for instance in some cases want to do something like this:
基本上,如果路径属性之前尚未初始化或者包含 null 或 false 或 0,它会初始化该属性。
换句话说(或其他代码):
与 messages[path] 相同,只是它使用某个对象而不是空数组来初始化它。
Basically it initialize the path property if it hasn't been initialized before or it contains null or false or 0.
In other words (or other code):
Same for messages[path] only it initializes it with some object and not empty array.
关键是理解逻辑 OR (
||
) 运算符。在 JavaScript 中,首先测试运算符左侧的表达式。如果解析为 true,则返回该表达式;如果解析为 false,则返回右侧的表达式。这是为变量分配默认值的好方法。其次,connections[path] 没有访问数组的元素。如果
path
解析为字符串,则connections[path]
相当于connections.path
,它只是访问的该成员连接对象。
连接[路径] = 连接[路径] || [];
编写此代码的人希望路径成员(如果没有别的)是一个空数组。此行本质上是检查
connections[path]
是否为 truthy 。如果该path
成员中有某些内容,它将被保留;如果不是,它将是一个空数组。消息[路径] = 消息[路径] || { version: 0, body: '' };
其功能与上一行相同,只是默认值是一个包含两个成员的对象而不是一个空数组。
The key is to understanding the logical OR (
||
) operator. In JavaScript, the expression to the left of the operator is tested first. If it resolves to true, that expression is returned; if it resolves to false, the expression on the right is returned. This is a great way of assigning default values to a variable.Secondly,
connections[path]
is not accessing an element of an array. Ifpath
resolves to a string, thenconnections[path]
is equivalent toconnections.path
, it's simply accessing that member of theconnections
object.connections[path] = connections[path] || [];
The person writing this code wanted the path member to be, if nothing else, an empty array. This line is essentially checking if
connections[path]
is truthy. If there is something in thatpath
member, it will be left alone; if not, it will be an empty array.messages[path] = messages[path] || { version: 0, body: '' };
This functions the same way as the previous line except the default value is an object with two members in it rather than an empty array.
大致翻译为
在这种情况下,这是为
connections[path]
设置默认值的便捷方法(即,如果尚未设置值,则将值设置为[]
)在某些语言(例如 Ruby)中,这可以写得更短
roughly translates to
In this case this is a convenient way to set default value for
connections[path]
(that is, set value to[]
if no value is already set)In some languages (Ruby, for example) this could be written even shorter
||
运算符的意思是 OR,因此connections[ path ] =connections[ path ] || [];
我们正在执行检查,看看是否
connections[ path ] === null
或connections[ path ] === undefined
,如果是我们将connections[path]
分配给一个空数组。对于
messages[ path ]
,我们执行相同的检查,如果它为 null 或未定义,我们为其分配{ version: 0, body: ''}
的值The
||
operator means OR so withconnections[ path ] = connections[ path ] || [];
we are performing a check to see if
connections[ path ] === null
or ifconnections[ path ] === undefined
, if this is the case we assignconnections[ path ]
to an empty array.With
messages[ path ]
we perform the same check and if it is null or undefined we assign it the value of{ version: 0, body: ''}
例如,让连接和消息为如下对象。
现在下面的代码的工作原理如下:
1.path 如果不存在于上述对象中,则分配空白字符串。
2.同样,消息的路径将是对象“Path”中存在的路径。否则,它将是“版本”和“主体”连接的对象
,而消息只是 JavaScript 对象的 JSON 表示形式。因此,connections[path] 与connections.path 相同。标志||如果对象中实际值不存在或未定义,则用于分配空白值。
For example, Let the connection and messages be the objects as below.
Now the code below works as below:
1.path is assigned blank string if it is not present in object mentioned above.
2.Similarly, Path of messages will be path present in the object 'Path'. Otherwise, it will be an object of 'version' and 'body'
connections and messages are just a JSON representation of javascript object. So, connections[path] is same as connections.path. Sign || used to assign blank values if actual value is not present or undefined in the object.
我应该激发你的想象力
|| b 返回第一个不包含任何类型的假值的元素。
I should to provoke your imagination
a || b returns first element which not contain an any kind of false value.