如何在 Javascript 中测试相同的对象实例?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26214966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 05:40:43  来源:igfitidea点击:

How to test same object instance in Javascript?

javascriptinstance

提问by Jér?me Verstrynge

Say I have the following objects in Javascript:

假设我在 Javascript 中有以下对象:

var a = { xxx: 33 };
var b = { xxx: 33 };
var c;

c = a;

What is the Javascript test that will tell me whether I am dealing with the same object instance? In other words, it should return false for a and b, b and c, but true for a and c.

什么 Javascript 测试会告诉我我是否正在处理相同的对象实例?换句话说,它应该对 a 和 b、b 和 c 返回 false,但对 a 和 c 返回 true。

回答by Amit Joki

You just need this

你只需要这个

if(c == a) {
   // same instance
}

a == band b == cwill return false

a == b并且b == c会回来false

回答by Quentin

Just a standard equality test:

只是一个标准的平等测试:

( a == c ) // true
( a == b ) // false

回答by CodingYoshi

I know the question is about checking if two objects are the same instance but this thread will not be complete without the following.

我知道问题是关于检查两个对象是否是同一个实例,但如果没有以下内容,该线程将不完整。

If you are after checking whether 2 objects are the same, a double equal ==is enough. However, for value types (primitives), you may be in a for surprise. Check out the following:

如果您在检查 2 个对象是否相同之后,则双重相等==就足够了。但是,对于值类型(原语),您可能会大吃一惊。查看以下内容:

var a = 1;  // Integer 1
var b = '1' // String '1'
if (a == b) console.log('Same.');
else console.log('Not same.');

Above will print Same.. To avoid that, use the triple equal ===which means two things:

上面会打印Same.。为避免这种情况,请使用三等号===,这意味着两件事:

  1. Are they the same value?
  2. Are they the same type?
  1. 它们的值相同吗?
  2. 它们是同一类型吗?

Or you can use Object.ismethod like Object.is(a, b).

或者你可以使用Object.isObject.is(a, b).

if (a === b) console.log('Same.');
else console.log('Not same.');
if (Object.is(a, b)) console.log('Same for Object.is.');
else console.log('Not same for Object.is.'); 

Above will print Not same.and Not same for Object.is..

以上将打印Not same.Not same for Object.is.

Some More Info

更多信息

Below is some more information which has been copy/pasted from this article:

以下是从本文复制/粘贴的更多信息:

Which operation you choose depends on what sort of comparison you are looking to perform. Briefly:

  • double equals (==) will perform a type conversion when comparing two things, and will handle NaN, -0, and +0 specially to conform to IEEE 754 (so NaN != NaN, and -0 == +0);

  • triple equals (===) will do the same comparison as double equals (including the special handling for NaN, -0, and +0) but without type conversion; if the types differ, false is returned.

  • Object.is does no type conversion and no special handling for NaN, -0, and +0 (giving it the same behavior as === except on those special numeric values).

Note that the distinction between these all have to do with their handling of primitives; none of them compares whether the parameters are conceptually similar in structure. For any non-primitive objects x and y which have the same structure but are distinct objects themselves, all of the above forms will evaluate to false.

您选择哪种操作取决于您要执行的比较类型。简要地:

  • double equals (==) 将在比较两个事物时执行类型转换,并将专门处理 NaN、-0 和 +0 以符合 IEEE 754(因此 NaN != NaN,并且 -0 == +0);

  • 三重等号 (===) 将执行与双等号相同的比较(包括对 NaN、-0 和 +0 的特殊处理),但不进行类型转换;如果类型不同,则返回 false。

  • Object.is 不进行类型转换,也不对 NaN、-0 和 +0 进行特殊处理(除了那些特殊的数值外,它的行为与 === 相同)。

请注意,这些之间的区别都与它们对原语的处理有关;他们都没有比较参数在结构上在概念上是否相似。对于任何具有相同结构但本身是不同对象的非原始对象 x 和 y,上述所有形式都将评估为假。



Conclusion

结论

My two cents on this would be to get into the habit of always using triple equals ===because it cannot hurt youbut double equals ==can indeed hurt you. You may use double equals ==when you just care about the value and the thing you are comparing are primitives; but even this is discouraged because it mostly means a flaw in design. Languages like TypeScript will even avoid that i.e. comparing a '1' to 1 without being explicit about it.

我的两个在这个美分将进入一直使用三层平等的习惯===,因为它不会伤害你,但双等于==确实伤害你==当您只关心值并且您比较的对象是原始值时,您可以使用双等号;但即使这样也是不鼓励的,因为它主要意味着设计上的缺陷。像 TypeScript 这样的语言甚至会避免这种情况,即在没有明确说明的情况下将“1”与 1 进行比较。