由于connections
andmessages
是对象,[
and]
不表示数组索引,而是访问成员的语法,其中变量用于查找变量的名称。
var path = 'x'
connections[path] = connections[path] || [];
上面,因为path = 'x'
等于
connections.x = connections.x || [];
就是说,如果两个对象的值所命名的成员path
存在,则保留它(将其当前值分配给自身),否则(||
此处用于合并)分别创建一个新的空数组或一个新{ version: 0, body: '' }
的 。
请注意,合并/逻辑或可以很容易地从左到右链接。例如,在某些情况下,您可能想要做这样的事情:
function createItem(color) {
// if a color is specified in the arguments, use that
// otherwise, if a color is specified in the settings object for this instance
// of the current user control, use that. otherwise, fall back to the global
// default values. if none of these settings have been defined, use black.
color = color || instanceSettings.defaultColor
|| globalSettings.defaultColor
|| '#000000';
...
}