更新时间:2023-11-29 08:42:58
是的,这是检查两者的好方法。语言规范陈述(关于 ==
用于比较不同类型的值):
Yes, that's a nice way to check for both. The language specification states (regarding ==
for comparing values of different types):
2)如果x为null且y为undefined,返回true。
2) If x is null and y is undefined, return true.
3)如果x未定义且y为null,则返回true。
3) If x is undefined and y is null, return true.
此处 x 和 y 是比较的条款 x == y
。当您比较 x == null
时,只有 x
时才会为真undefined
,或 null
本身。
Here x and y are the terms of a comparison x == y
. When you're comparing x == null
, it will only be true if x
is undefined
, or null
itself.
为了清楚起见,当我们在这里说未定义时,我们指的是值 undefined
,而不是未定义的变量(无论何时使用它们都会产生ReferenceError,除了 typeof
)。
Just to be clear, when we say "undefined" here we mean the value undefined
, not variables that are not defined (those produce a ReferenceError whenever they're used, except with typeof
).
关于WWDCD,我'引用Ian: Crockford会建议JSLint建议(显然),因为==对他来说是邪恶的。这意味着使用 ===
,绝不会 ==
。所以这对他来说不是问题。
And regarding WWDCD, I'll quote Ian: Crockford would suggest what JSLint suggests (obviously) because == is voodoo to him. That means "use ===
, never ==
". So this would be a non-question for him.