The [object Object] here is obtained due to the implicit type conversion of myObject in this step myObject[myObject] = "foo";, so the [object Object] here is just a converted string, but in the object as '[object Object]' stores the value 'foo' as the key!
This is an implicit conversion problem of object key names. Take this example, explained in vernacular:
There is an A object, and I want to pass the B object in as a key name of the A object, but because all the key names of the object are strings, the B object will call the toString() method and convert it to a string "[ object Object]" to store data as a key name of an A object. The data in the instance is this "foo" string.
Another point that needs special attention is: The A object and B object in your question are the same, which is very confusing, so I declared a new mykey to distinguish them.
var myObject = {};
var myKey={};
myObject[myKey]="foo";
console.log("[object Object]"===myKey.toString());//true
The printed result is true, which proves that "[object Object]" is not equal to {}, but is equal to the string returned by the object after passing the toString() method
The [object Object] here is obtained due to the implicit type conversion of myObject in this step
myObject[myObject] = "foo";
, so the [object Object] here is just a converted string, but in the object as '[object Object]' stores the value 'foo' as the key!This is an implicit conversion problem of object key names.
Take this example, explained in vernacular:
There is an A object, and I want to pass the B object in as a key name of the A object, but because all the key names of the object are strings, the B object will call the toString() method and convert it to a string "[ object Object]" to store data as a key name of an A object. The data in the instance is this "foo" string.
Another point that needs special attention is:
The A object and B object in your question are the same, which is very confusing, so I declared a new mykey to distinguish them.
The key value will be automatically converted into string