Reputation: 29
I have the following code snippet below:
var obj = new Object();
var foo = new Object();
var bar = new Object();
obj[foo] = 'hello';
obj[bar] = 'hi'
console.log (obj[foo])
It prints "hi". Why is this?
Upvotes: 0
Views: 84
Reputation: 22921
You're defining foo
as an object, and bar
as an object. When you assign them to the original object (obj
), you're basically saying this:
obj["Object"] = 'hello';
obj["Object"] = 'hi';
console.log(obj["Object"]); //Will be the last object.
Keys need to be string types. If they're not a string, they will be converted to a string. In the cast of Object()
to a string, they'll be turned into [object Object]
. Check out this answer for an alternatitve way to override toString()
's functionality to return the object's name, or a unique identifer for the key you're trying to create.
To see this in action, let's do this:
var obj = new Object();
var foo = new Object();
var bar = new Object();
obj[foo] = 'hello';
console.log(obj);
/* Shows:
{
"[object Object]": "hello"
}
*/
obj[bar] = 'hi';
console.log(obj);
/* Shows:
{
"[object Object]": "hi"
}
*/
console.log(obj[foo]); //Will show hi
console.log(obj['[object Object]']); //Essentially the same thing
Upvotes: 1
Reputation: 906
you are assigning the object obj with keys which are objects (foo & bar).
When keys given to object are objects themselves then the following is assigned as the key value
[object Object]
this simply means that your 1st assignment
obj[foo] = 'hello'
created key [object Object] and made its value "hello";
then your second assignment did the same, but since the key [object Object] is already present, the previous value got overridden with "hi".
Now when you try to access the property of obj with foo or bar or any other object, you are actually doing
obj["[object Object]"]
Upvotes: 1
Reputation: 1968
You are using objects as property-names in your object obj
. Property names are strings, therefore your objects are stringified to [object Object]
. As both objects are stringified to the same name, bar
replaces foo
.
The result looks like this:
{
"[object Object]": "hi"
}
Upvotes: 0
Reputation: 1748
Objects in JS can have string keys only. When you do obj[foo]
actualy you do obj[foo.toString()]
.
Your code will be
obj["[object Object]"] = 'hello';
obj["[object Object]"] = 'hi'
console.log (obj["[object Object]"])
Upvotes: 3
Reputation: 1
I have not used the new object constructor on javascript yet, since it is not my preferred language, but the mistake is most likely obvious, you have not assigned anything on your "foo" and "bar" variables, so regardless, their values will be the same, which is the initial value upon declaration of the object without any assignment. What's happening is you are assigning second value on top of your first, which applies to the "obj" array's element.
Upvotes: 0
Reputation: 386868
Objects need a string as reference. Any variable used as key is casted to string with the prototype toString
and here you get for this '[object Object]'
, which is used as key.
var obj = new Object(),
foo = new Object(),
bar = new Object();
obj[foo] = 'hello';
obj[bar] = 'hi'
console.log (obj[foo]);
console.log('' + foo); // force object to use toString() method
console.log (obj['[object Object]']);
Upvotes: 2