How to test same object instance in Javascript?

霸气de小男生 提交于 2019-12-04 15:36:12

问题


Say I have the following objects in 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.


回答1:


You just need this

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

a == b and b == c will return false




回答2:


Just a standard equality test:

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


来源:https://stackoverflow.com/questions/26214966/how-to-test-same-object-instance-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!