本文翻译自:variable === undefined vs. typeof variable === “undefined”
The jQuery Core Style Guidelines suggest two different ways to check whether a variable is defined. jQuery Core Style Guidelines提出了两种不同的方法来检查变量是否已定义。
- Global Variables:
typeof variable === "undefined"
全局变量:typeof variable === "undefined"
- Local Variables:
variable === undefined
局部变量:variable === undefined
- Properties:
object.prop === undefined
属性:object.prop === undefined
Why does jQuery use one approach for global variables and another for locals and properties? 为什么jQuery为什么对全局变量使用一种方法而对局部变量和属性使用另一种方法?
#1楼
参考:https://stackoom.com/question/jPlp/变量-未定义vs-typeof变量-未定义
#2楼
Who is interested in the performance gain of variable === undefined
, may take a look here, but it seems to be a chrome optimization only. 谁对variable === undefined
的性能提高感兴趣,可以在这里看看,但这似乎只是chrome优化。
- http://jsperf.com/type-of-undefined-vs-undefined/30 http://jsperf.com/type-of-undefined-vs-undefined/30
- http://jsperf.com/type-of-undefined-vs-undefined http://jsperf.com/type-of-undefined-vs-undefined
#3楼
typeof a === 'undefined'
更快然后a === 'undefined'
由节点v6.9.1约2倍。
#4楼
For local variables, checking with localVar === undefined
will work because they must have been defined somewhere within the local scope or they will not be considered local. 对于局部变量,可以使用localVar === undefined
检查,因为它们必须已在局部范围内定义,否则将不被视为局部变量。
For variables which are not local and not defined anywhere, the check someVar === undefined
will throw exception: Uncaught ReferenceError: j is not defined 对于不在局部且未在任何地方定义的变量,检查someVar === undefined
将引发异常: Uncaught ReferenceError:j未定义
Here is some code which will clarify what I am saying above. 这是一些代码,这些代码将阐明我在上面所说的内容。 Please pay attention to inline comments for further clarity . 请注意内联注释,以进一步澄清 。
function f (x) {
if (x === undefined) console.log('x is undefined [x === undefined].');
else console.log('x is not undefined [x === undefined.]');
if (typeof(x) === 'undefined') console.log('x is undefined [typeof(x) === \'undefined\'].');
else console.log('x is not undefined [typeof(x) === \'undefined\'].');
// This will throw exception because what the hell is j? It is nowhere to be found.
try
{
if (j === undefined) console.log('j is undefined [j === undefined].');
else console.log('j is not undefined [j === undefined].');
}
catch(e){console.log('Error!!! Cannot use [j === undefined] because j is nowhere to be found in our source code.');}
// However this will not throw exception
if (typeof j === 'undefined') console.log('j is undefined (typeof(x) === \'undefined\'). We can use this check even though j is nowhere to be found in our source code and it will not throw.');
else console.log('j is not undefined [typeof(x) === \'undefined\'].');
};
If we call the above code like this: 如果我们这样调用上面的代码:
f();
The output would be this: 输出将是这样的:
x is undefined [x === undefined].
x is undefined [typeof(x) === 'undefined'].
Error!!! Cannot use [j === undefined] because j is nowhere to be found in our source code.
j is undefined (typeof(x) === 'undefined'). We can use this check even though j is nowhere to be found in our source code and it will not throw.
If we call the above code like these (with any value actually): 如果我们像这样调用上面的代码(实际上有任何值):
f(null);
f(1);
The output will be: 输出将是:
x is not undefined [x === undefined].
x is not undefined [typeof(x) === 'undefined'].
Error!!! Cannot use [j === undefined] because j is nowhere to be found in our source code.
j is undefined (typeof(x) === 'undefined'). We can use this check even though j is nowhere to be found in our source code and it will not throw.
When you do the check like this: typeof x === 'undefined'
, you are essentially asking this: Please check if the variable x
exists (has been defined) somewhere in the source code. 当您进行如下检查: typeof x === 'undefined'
,您实际上是在问: 请检查变量x
是否在源代码中某处存在(已定义)。 (more or less). (或多或少)。 If you know C# or Java, this type of check is never done because if it does not exist, it will not compile. 如果您知道C#或Java,则永远不会进行这种类型的检查,因为如果不存在,它将无法编译。
#5楼
Because undefined
is not always declared, but jQuery declares undefined
in its main function. 因为undefined
并不总是声明,但是jQuery在其main函数中声明undefined
。 So they use the safe undefined
value internally, but outside, they use the typeof
style to be safe. 因此,他们在内部使用安全的undefined
值,但在外部,他们使用typeof
样式是安全的。
#6楼
For undeclared variables, typeof foo
will return the string literal "undefined"
, whereas the identity check foo === undefined
would trigger the error "foo is not defined" . 对于未声明的变量, typeof foo
将返回字符串文字"undefined"
,而身份检查foo === undefined
将触发错误“ foo not defined” 。
For local variables (which you know are declared somewhere), no such error would occur, hence the identity check. 对于局部变量(您知道已在某处声明),不会发生此类错误,因此进行身份检查。