marijnh / Eloquent-JavaScript

The sources for the Eloquent JavaScript book
https://eloquentjavascript.net
3.02k stars 796 forks source link

The "Object" error #526

Closed lcongdanh closed 4 years ago

lcongdanh commented 4 years ago

take a look at this code. i missed type "Object" instead of "object" i found out an interesting error

`function deepEqual(a, b) { if (a === b) return true; if (a == null || typeof a != "Object" || b == null || typeof b != "Object") { return false; } let keysA = Object.keys(a), keysB = Object.keys(b);

if (keysA.length != keysB.length) return false;

for (let key of keysA) { if (!keysB.includes(key) || !deepEqual(a[key], b[key])) { return false; } }

return true; }; let obj = { here: { is: "an" }, object: 2 }; console.log( deepEqual( { here: { is: "an" }, object: 2 }, { here: { is: "an" }, object: 2 } ) ); // => false console.log(deepEqual(obj, obj)); // => true ` as you can see i put the raw object the function return false but if i put both of them it in variables they return true

lcongdanh commented 4 years ago

i just found out the comparison "===" just compare the object references rather than their content