Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
359 views
in Technique[技术] by (71.8m points)

javascript - 在JavaScript中检查字符串相等性的正确方法是什么?(What is the correct way to check for string equality in JavaScript?)

在JavaScript中检查字符串之间是否相等的正确方法是什么?

  ask by JSS translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

always Until you fully understand the differences and implications of using the == and === operators, use the === operator since it will save you from obscure (non-obvious) bugs and WTFs.

( 总是 直到你完全理解的差异和使用的影响=====运营商,使用===运营商,因为它可以使你免于晦涩(非显而易见性)的错误和WTFs。)

The "regular" == operator can have very unexpected results due to the type-coercion internally, so using === is always the recommended approach.

(由于内部存在类型强制,“常规” ==运算符可能会产生非常意外的结果,因此始终建议使用=== 。)

For insight into this, and other "good vs. bad" parts of Javascript read up on Mr. Douglas Crockford and his work.

(为了了解这一点以及Javascript的其他“好与坏”部分,请阅读Douglas Crockford先生及其工作。)

There's a great Google Tech Talk where he summarizes lots of good info: http://www.youtube.com/watch?v=hQVTIJBZook

(他在Google技术讲座上精彩地总结了很多很好的信息: http : //www.youtube.com/watch?v=hQVTIJBZook)


Update:

(更新:)

The You Don't Know JS series by Kyle Simpson is excellent (and free to read online).

(凯尔·辛普森(Kyle Simpson) 撰写的您不知道JS”系列非常出色(可在线免费阅读)。)

The series goes into the commonly misunderstood areas of the language and explains the "bad parts" that Crockford suggests you avoid.

(该系列文章深入探讨了该语言中经常被误解的部分,并解释了克罗克福德建议您避免的“不良部分”。)

By understanding them you can make proper use of them and avoid the pitfalls.

(通过了解它们,您可以正确使用它们并避免陷阱。)

The " Up & Going " book includes a section on Equality , with this specific summary of when to use the loose ( == ) vs strict ( === ) operators:

(在“ 调试与展望 ”一书包括上一节平等 ,用时使用宽松(这个特定的总结== )与严格( === )运算符:)

To boil down a whole lot of details to a few simple takeaways, and help you know whether to use == or === in various situations, here are my simple rules:

(要将大量细节归结为一些简单的要点,并帮助您知道在各种情况下是使用==还是=== ,这是我的简单规则:)

  • If either value (aka side) in a comparison could be the true or false value, avoid == and use === .

    (如果比较中的任何一个值(也称为正值)可以是truefalse ,请避免==并使用=== 。)

  • If either value in a comparison could be of these specific values ( 0 , "" , or [] -- empty array), avoid == and use === .

    (如果比较中的任何一个值都可以是这些特定值( 0""[] -空数组),请避免==并使用=== 。)

  • In all other cases, you're safe to use == .

    (在所有其他情况下,您可以放心使用== 。)

    Not only is it safe, but in many cases it simplifies your code in a way that improves readability.

    (它不仅安全,而且在许多情况下都以提高可读性的方式简化了代码。)

I still recommend Crockford's talk for developers who don't want to invest the time to really understand Javascript—it's good advice for a developer who only occasionally works in Javascript.

(对于那些不想花时间真正理解Javascript的开发人员,我仍然推荐Crockford的演讲。这对于只偶尔使用Javascript的开发人员是一个很好的建议。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...