let obj1 = {
1: 1
};
let obj2 = {};
obj2[obj1] = 2;
let keys = Object.keys(obj2);
// first
for (let key of keys) {
console.log(key) // [object Object]
}
for (let prop in obj2) {
console.log(prop) // [object Object]
}
let key = keys[0];
// second
console.log(typeof key); // string
console.log(JSON.stringify(key) === JSON.stringify(obj1)); // false
// thirth
console.log(obj2['[object Object]']); // 2
obj2[{}] = 3;
// fourth
console.log(obj2['[object Object]']); // 3
console.log(obj2[obj1]); // 3
I have 4 questions:
1/. In the first: is there a way to get object
{
1: 1
}
instead of [object object]
?
2/. In the second: why am I getting string
when trying to get the type of an object (not object
)?
3/. In the thirth: the key of an object is an object. So, why can I assign it via a string?
4/. In the fourth: after adding another object to obj2
, obj1
has been overridden although {}
is different from obj1
(not duplicate key). Why?