1

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?

Blue
  • 22,608
  • 7
  • 62
  • 92
  • Wrong terminology. There is no array in your code, just three objects. Using the brackets notation has nothing to do with arrays. – str Aug 26 '16 at 10:26
  • If one of the answers below answered your question, the way this site works works, you'd "accept" the answer, more here: ***[What should I do when someone answers my question?](http://stackoverflow.com/help/someone-answers)***. But only if your question really has been answered. If not, consider adding more details to the question. – Blue Aug 30 '16 at 09:19

6 Answers6

4

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]"]) 
Maxx
  • 1,740
  • 10
  • 17
2

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]']);

   
   
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

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
Community
  • 1
  • 1
Blue
  • 22,608
  • 7
  • 62
  • 92
1

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]"]
Himanshu Tanwar
  • 906
  • 6
  • 18
0

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.

0

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"
}
PerfectPixel
  • 1,918
  • 1
  • 17
  • 18