javascript - Get key of an object when the key is an object -


let obj1 = {      1: 1  };    let obj2 = {};  obj2[obj1] = 2;    let keys = object.keys(obj2);    // first  (let key of keys) {    console.log(key) // [object object]  }    (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 first: there way object

{     1: 1 } 

instead of [object object]?

2/. in second: why getting string when trying type of object (not object)?

3/. in thirth: key of object object. so, why can assign via string?

4/. in fourth: after adding object obj2, obj1 has been overridden although {} different obj1 (not duplicate key). why?

javascript objects can use strings keys. thus, obj2[obj1] = 2 equivalent obj2[obj1.tostring()] = 2; , obj1.tostring() "[object object]". thus, obj2 is, in fact, { "[object object]": 2 }.

  1. you can't isn't there.

  2. because isn't object.

  3. no, isn't.

  4. {}.tostring() same ({ 1: 1 }).tostring(), so... can see leads.


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -