I am looking for a way to check if two variables refer to the same object in javascript.
ex:
var a = {foo:"bar"};
var b = {foo:"bar"};
var aa = a;
testSame(a, b); //false
testSame(a, aa); //true
var r = "a string";
var s = "a string";
var rr = r;
testSame(r, s); //false
testSame(r, rr); //true
So far there doesn't seem to be a way of getting a way of doing this.
edit: testSame()
is not a real function it would be the hypothetical solution.
edit: The answer to the question How to check if two vars have the same reference? does not answer this question as it uses the strict equality operator (===
) which does not differentiate between between 2 vars pointing to 2 instances of an identical string.