简体   繁体   English

检查undefined / null时可以使用类型强制吗?

[英]OK to use type coercion when checking for undefined/null?

Is it acceptable to use type coercion (== instead of ===) to check for undefined/null? 是否可以使用类型强制(==而不是===)来检查undefined / null?

What are the downsides? 有什么缺点? Is there a better way to check for undefined/null? 有没有更好的方法来检查undefined / null?

Clarification: I am looking for a statement that will check for both undefined and null. 澄清:我正在寻找一个将检查undefined和null的语句。

test(null);
test(undefined);

function test(myVar) {
    if (myVar != undefined) {
        alert('never gets called')
    }
}

== undefined and == null are both equivalent to checking if the value is either undefined or null, and nothing else. == undefined== null 都等同于检查值是未定义还是null,而不是其他任何内容。

As for "acceptable", it depends on who is looking at the code, and whether your linter is configured to ignore this special case. 至于“可接受”,它取决于谁在查看代码,以及你的linter是否配置为忽略这种特殊情况。

I can tell you for sure that some minifiers already do this optimisation for you. 我可以肯定地告诉你,一些缩小器已经为你做了这个优化。

I'm going to attempt to address this question as objectively as possible, but it deals with some mushy "best practices" opinion stuff that can trigger people to forget that there's more than one way to do things. 我将尝试尽可能客观地解决这个问题,但它会处理一些糊涂的“最佳实践”意见,这些意见可能会引发人们忘记执行操作的方法不止一种。

Is it acceptable to use type coercion ( == instead of === ) to check for undefined / null ? 是否可以使用类型强制( ==而不是=== )来检查undefined / null

It's acceptable to write code however you see fit regardless of anyone who tells you otherwise. 编写代码是可以接受的,不管有谁告诉你,你认为合适。 Including me. 包括我。

That's not really helpful here, so let me expand on that a bit, and I'll let you decide how you feel about it. 这在这里并没有多大帮助,所以让我稍微扩展一下,我会让你决定你的感受。

Code is a tool that can be used to solve problems, and == is a tool within JavaScript code to solve a specific problem . 代码是一种可用于解决问题的工具,而==是JavaScript代码中用于解决特定问题的工具 It has a well-defined algorithm which it follows for checking a type of equality between two parameters. 它有一个定义明确的算法 ,它用于检查两个参数之间的相等类型。

=== , on the other hand, is a different tool that solves a different , specific problem . 另一方面, ===是一种解决不同的 特定问题不同工具。 It also has a well-defined algorithm which it follows for checking a type of equality between two parameters. 它还有一个定义明确的算法 ,用于检查两个参数之间的相等类型。

If one tool or the other is appropriate for your use-case, then you shouldn't hesitate to use the tool. 如果一个工具或另一个工具适合您的用例,那么您应该毫不犹豫地使用该工具。

If you need a hammer, use a hammer. 如果你需要锤子,请使用锤子。

But if all you have is a hammer, then everything looks like a nail , and this is the core issue with == . 如果你拥有的只是一把锤子,那么一切看起来都像钉子一样 ,这是==的核心问题。 Developers coming from other languages often aren't aware of how == works and use it when === would be appropriate. 来自其他语言的开发人员通常不知道==如何工作并在===适当时使用它。

In fact, most use cases are such that === should be preferred over == , but as you've asked in your question: that's not true in this instance. 实际上,大多数用例都是===应该优先于== ,但正如你在问题中提到的那样:在这种情况下并非如此。

What are the downsides? 有什么缺点?

If you relax your standards for === and allow developers to use == you may very well get bitten by misuse of == , which is why sticking religiously to === may be acceptable to some, and seem foolish to others. 如果你放宽了===的标准并且允许开发人员使用==你可能会因滥用==而被咬伤,这就是为什么坚持认为===可能是某些人可以接受的,而对其他人来说似乎是愚蠢的。

if (val === null || val === undefined) {...

is not particularly difficult to write, and is very explicit about intent. 写作并不是特别困难,而且对意图非常明确。

Alternatively, 或者,

if (val == null) {...

is much more concise, and not difficult to understand either. 更简洁,也不难理解。

Is there a better way to check for undefined / null ? 有没有更好的方法来检查undefined / null

"better" is subjective, but I'm going to say, "no" anyway because I'm not aware of any that improve the situation in any meaningful way. “更好”是主观的,但我会说,“不”,因为我不知道有任何改善情况的任何有意义的方式。

At this point I will also note that there are other checks that could be performed instead of null / undefined . 此时我还会注意到可以执行其他检查而不是 null / undefined Truthy/falsey checks are common, but they are another tool used to solve yet another specific problem : Truthy / falsey检查很常见,但它们是用于解决另一个特定问题的另一种工具:

if (!val) {...

is much more concise than either of the previous options, however it comes with the baggage of swallowing other falsey values. 比以前的任何一个选项都简洁得多,但它带有吞咽其他虚假价值的包袱。


Additional notes on enforcement via linting: 关于通过linting实施的附加说明:

If you follow the strictness of Crockford's JSLint , == is never tolerated, in the understanding that a tool that's "sometimes useful" but "mostly dangerous" isn't worth the risk. 如果你遵循Crockford的JSLint的严格性, == 永远不会被容忍,理解为“有时有用”但“大多数危险”的工具不值得冒险。

ESLint , on the other hand, allows for a more liberal interpretation of the rule by providing an option to allow null as a specific exception. 另一方面, ESLint通过提供允许null作为特定异常的选项,允许对规则进行更自由的解释。

The ideal way to test for undefined is using typeof . 测试undefined的理想方法是使用typeof Note you only need to test them separately if you care about a difference between undefined and null : 请注意,如果您关心undefinednull之间的区别,则只需要单独测试它们:

test(null);
test(undefined);

function test(myVar) {
    if (typeof myVar === "undefined") {
        // its undefined
    }
    if (myVar === null) {
        // its null
    }
}

If you don't care if its undefined / null , then just do: 如果你不关心它的undefined / null ,那么就这样做:

test(null);
test(undefined);

function test(myVar) {
    if (!myVar) {
        // its undefined or null, dunno which, don't care!
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM