For convenience I wrote a simple toJSON
prototype, for handling JSON that I know to be safe:
String.prototype.toJSON = function () {
return JSON.parse(this.valueOf());
};
I am using it in testing my web-services. Unfortunately even with this simple test:
var v0 = '{"echo":"hello_world"}'.toJSON(), v1 = {"echo": "hello_world"};
It fails:
console.log(v0 == v1); // false
console.log(v0 === v1); // false
console.log(v0.echo == v1.echo); // true
console.log(v0.echo === v1.echo); // true
What do I not know about JavaScript which is causing this issue?
{a:1} !== {a:1}
. Two objects are the same... if they are the same one, and point to the same object in the heap. – Bilabial