【发布时间】:2014-09-11 01:43:05
【问题描述】:
我在 Chrome 中查看 JavaScript 控制台并注意到一些奇怪的东西,尽管它们看起来相同,obj 和 JSON.parse(JSON.stringify(obj)) 并不相同。这是为什么呢?
var obj = {test:'this is a test', another: {omg:'ay dios mio', check:true}};
console.log(obj, JSON.parse(JSON.stringify(obj)));
console.log(obj == JSON.parse(JSON.stringify(obj)));
它们看起来相同,但在检查相等性时返回 false。这是为什么呢?
【问题讨论】:
-
不是真的重复,但那里的答案也回答了你的问题。
-
如果您将两者都转换为字符串
JSON.stringify(),那么您会看到它们的值是相等的。 -
MDN: Equality operators
[...]If both operands are objects, then JavaScript compares internal references which are equal when operands refer to the same object in memory.
标签: javascript json object