我对JavaScript很新。以下代码来自一些生产代码库。
regDefinition以JSON形式传递。但是我不太确定方法体中的语法。
特别是||
和[]
部分。
function getCookieValue(regDefinition) {
return (document.cookie.match(regDefiniation.regEx) || [])[regDefiniation.index] || null;
}
我对JavaScript很新。以下代码来自一些生产代码库。
regDefinition以JSON形式传递。但是我不太确定方法体中的语法。
特别是||
和[]
部分。
function getCookieValue(regDefinition) {
return (document.cookie.match(regDefiniation.regEx) || [])[regDefiniation.index] || null;
}
看起来有人很用心地让这很难读懂。
如果我理解正确,它做的事情类似于以下内容:
regDefiniation
拼对! - techfoobarmatch
总是会返回一个数组。然后代码失败了,有人通过添加 || []
部分进行了热修复。无论原因如何,在添加一行注释甚至更好的情况下,花费 3 或 5 行代码来使其可读是没有任何羞耻感的。 - GolezTrolstuff() || default
是Javascript中的一种惯用表达式,一旦习惯了阅读它并不难理解。增加一些空格会更有帮助,但我认为将其分成if
和else
语句对于大多数Javascript程序员来说过于冗长。 - Fengyang Wangvar matches = document....match(..) || []; return matches[index] || null;
,它已经更易读了,同时没有if语句,只有两行代码,仍然具有相同的表达式。 - GolezTrol这里有一些好的回答,但似乎没有人真正解释为什么要这样做。
(foo || [])[bar]; // or similarly (foo || {})[bar]
不仅仅是
foo[bar]
考虑正则表达式RegExp匹配失败的情况。
var foo = null, bar = 0;
如果没有任何特殊处理,你将会收到一个错误并且代码会停止运行。
foo[bar]; // TypeError: Cannot read property '0' of null
然而带括号的或运算符版本将不同。
(foo || [])[bar]; // undefined (note, no error)
这是因为(null || [])
的结果是[]
,所以现在您可以安全地尝试读取它的属性。
|| []
是为了防止 match
没有返回任何内容。特别是 @Wyzard 的(被低估的)答案非常清楚地解释了这一点。但我们两个都没有详细说明会出现什么样的错误,所以对此表示赞赏。 - GolezTroldocument.cookie
是一个包含当前页面的 cookie 的字符串。调用 document.cookie.match(regDefiniation.regEx)
函数使用正则表达式搜索这个字符串,以获取与之匹配的子字符串列表。match
调用将返回 null,所以 || []
用于将该 null 替换为一个空数组。这确保了表达式 (document.cookie.match(regDefiniation.regEx) || [])
总是返回一个数组。
[regDefiniation.index]
只是从该数组中检索一个元素。但是,如果请求的索引在数组中不存在(例如,如果数组为空因为正则表达式在 cookie 字符串中没有找到任何内容),结果将是 undefined
,所以 || null
将在这种情况下将结果更改为 null
。因此,要理解这个例子,让我们深入了解一下。
var myValue = someValue || otherValue
// Values that evaluate to false:
false
"" // An empty string.
NaN // JavaScript's "not-a-number" variable.
null
undefined // Be careful -- undefined can be redefined!
0 // The number zero.
var myCookie = document.cookie.match(regDefiniation.regEx) || []
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators
{}
。 - The amateur programmerdocument.cookie
返回一个字符串,然后应用 match
方法(内置)。如果参数在 regDefiniation.regEx
中找到,则执行此操作,否则返回 []
(即数组)。
接下来,将上一步返回的任何内容与 [regDefiniation.index]
进行索引。