javascript中的平等 [英] equality in javascript

查看:43
本文介绍了javascript中的平等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在javascript中工作时,有人能为我提供关于测试平等/不平等和类型强制的良好参考或解释吗?

When working within javascript can someone provide me a good reference or explanation over testing for equality/inequality and type coercion?

从我读过的内容中我看到使用eqeq(==)与eqeqeq(===)有两个思路的原则,有些人认为你应该不使用eqeq并始终使用eqeqeq因为它更安全。

From what I have been reading I see where there are two principles of thought on using eqeq (==) vs. eqeqeq (===) some feel that you should not use eqeq and always as use eqeqeq as it is safer to use.

我一直在玩一些基本的样品,我无法辨别差异或最好使用其中一种:

I have been playing around with some basic samples and I am having trouble discerning the difference or when best to use one over the other:

例如:这是我写的一些基本脚本。当我使用eqeq或eqeqeq测试时,我得到相同的结果。我还没有看到一个例子,我会得到不同的结果(即使用eqeq返回true,其中eqeqeq返回false)。

For example: here is some basic script I was writing out. When I test using eqeq or eqeqeq I get the same result. I have not seen an example yet where I would get a different results (ie. using eqeq returns true where eqeqeq returns false).

function write(message){

  document.getElementById('message').innerHTML += message +'<br/>';
}


var tim = { name: "tim" };
var tim2 = { name: "tim" };
//objects are equal to themselves ( == vs ==== eqeq or eqeqeq)
write("tim eq tim: " + (tim == tim)); //returns true

//objects are only equal to themselves regardless of containing value got that
write("tim eq tim2: " + (tim === tim2)); //returns false

//access the primative type to test true or false
write("tim value eq tim2 value: " + (tim.name === tim2.name)); //returns true
//how does this differ in efficency over the eqeq operator? is one safer to use over the other?
//write("tim value eq tim2 value: " + (tim.name == tim2.name)); //also returns true

//testing primatives

write("apple eqeqeq apple: " + ("apple" === "apple"));  //true
write("apple eqeqeq apple: " + ("apple" == "apple"));  //true

有人可以提供我可以阅读的解释或参考,以帮助澄清这一点。

Can someone provide an explanation or reference I can read that helps clarify this a bit more.

欢呼,

推荐答案

==和==之间的区别=非常简单:==是价值的比较。 ===是价值和类型的比较。使用===将阻止JavaScript动态确定类型并完全按原样比较值。

The difference between == and === is fairly simple: == is a comparison of value. === is a comparison of value and type. Using === will prevent JavaScript from dynamically determining type and compares values exactly as they are.

5 == "5"     //true - JS compares the number 5 to a string of "5" and determines the contents are the same 
5 === "5"    //false - A character is not a number, they can't be the same.

0 == false   //true - false is a bool, 0 is numerical, but JS determines that 0 evaluates to false
0 === false  //false - numeric is not boolean, they can't be exactly the same thing

5 == 5.0     //true - need I say more?
5 === 5.0    //true - one is a float and one is an int, but JS treats them all as numerical

我发现对于具有可以返回false(失败)和0(作为合法结果)的函数的测试至关重要。

I find it's critical for tests with functions that can return both false (for failure) and 0 (as a legitimate result).

JS总共有5种基本类型= numeric,string,boolean,null和undefined。 ===要求两个参数具有相同的类型相等的值才能返回true。没有float,int,long,short等等 - 任何类型的数字都被归为一个数字。

JS has a total of 5 primitive types = numerical, string, boolean, null and undefined. === requires both arguments to be of the same type and equal value to return true. There are no float, int, long, short, etc - any type of number is lumped together as numerical.

这篇关于javascript中的平等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆