变量===未定义vs. typeof变量===“未定义”

本文翻译自: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优化。


#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,则永远不会进行这种类型的检查,因为如果不存在,它将无法编译。

<== Fiddle Me ==> <==摆弄我==>


#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. 对于局部变量(您知道已在某处声明),不会发生此类错误,因此进行身份检查。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值