设置Javascript变量,var win = window.dialogArguments ||是什么揭幕战||父母||最佳;做? [英] Setting Javascript Variable, what does var win = window.dialogArguments || opener || parent || top; do?
问题描述
我在Javascript文件中遇到过这种结构,我不确定它是做什么的。
I came across this construction in a Javascript file, and I'm not sure what it does.
var win = window.dialogArguments || opener || parent || top;
我理解 window.dialogArguments
返回可选来自父窗口的参数,但我不明白 ||
运算符正在做什么。
I understand that window.dialogArguments
returns optional arguments from the parent window, but I do not understand that the ||
operators are doing.
这是设置某种层次结构,如果 .dialogArguments
返回NULL,它将分配值 window.opener
而不是(依此类推)?
Is this setting up some sort of hierarchy, where if .dialogArguments
returns NULL, it will assign the value of window.opener
instead (and so on)?
推荐答案
< a href =https://developer.mozilla.org/en/JavaScript/Reference/Operators/Logical_Operators =nofollow> ||
运算符是逻辑OR运营商。但它的评估不返回布尔值( true / false ),但第一个操作数的值与 true $ c相同$ c> 转换为布尔值(即
布尔值(op)=== true
)否则为第二个操作数的值。
The ||
operator is the logical OR operator. But its evaluation does not return a boolean value (true/false) but the first operand’s value that is identical to true
when converted to boolean (i.e. Boolean(op) === true
) or the second operand’s value otherwise.
所以表达式 window.dialogArguments | |揭幕战||父母||顶部
,相当于 window.dialogArguments || (开瓶器||(父||顶))
,将产生:
So the expression window.dialogArguments || opener || parent || top
, that is equivalent to window.dialogArguments || (opener || (parent || top))
, will yield:
- <$ c的值$ c> window.dialogArguments 如果其值转换为boolean等于
true
,或 - 的值
opener
如果其值转换为boolean等于true
,或 - 该值
parent
如果其值转换为boolean等于true
,或 - 价值
顶部
否则。
- the value of
window.dialogArguments
if its value converted to boolean equalstrue
, or - the value of
opener
if its value converted to boolean equalstrue
, or - the value of
parent
if its value converted to boolean equalstrue
, or - the value of
top
otherwise.
所以它实际上相当于:
var win;
if (window.dialogArguments) {
win = window.dialogArguments;
} else if (opener) {
win = opener;
} else if (parent) {
win = parent;
} else {
win = top;
}
请注意,如果以下情况,您可能会收到 ReferenceError 变量不存在。如果您在全局范围内(窗口
),最好使用 window.foo
而不是 foo
以避免此类 ReferenceError 。
Note that you might get a ReferenceError if the variable does not exist. If you’re in the global scope (window
), better use window.foo
instead of just foo
to avoid such ReferenceErrors.
顺便说一句:其他语言具有类似的操作和功能几乎一样,例如SQL的 coalesce
函数确实返回第一个非 NULL
值。
By the way: Other languages have similar operations and functions that do nearly the same, e.g. SQL’s coalesce
function that does return the first non-NULL
value.
这篇关于设置Javascript变量,var win = window.dialogArguments ||是什么揭幕战||父母||最佳;做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!