9

What is going on in this code?

var a = {a:1};
var b = {b:2};
var c = {};

c[a] = 1;
c[b] === 1 // true!

c[b] = 2;
c[a] === 2 // true!

Specifically, why does using looking up b in c return the value that was stored in a property of a?

What does it mean to use an object as a key to a property in JavaScript?

I've tested this in Chrome/Node and in Firefox.

4
  • 2
    currently javascript does not have support for object as a key it probably get coherse (converted to) string which will be [object Object] and store the value hence it gives true to every object as a key, but in future 'Map' is something you can use to store object as a key. Commented Sep 12, 2012 at 10:49
  • Sounds interesting @Sushil. Do you have a reference to information about this potential future Map class? I couldn't find it mentioned anywhere after a few minutes of searching. Commented Sep 12, 2012 at 11:05
  • 1
    its a future implementation for ECMAScript 6 developer.mozilla.org/en-US/docs/JavaScript/Reference/… here u go and additionally there is a very nice WeakMap concept (i personally like it) developer.mozilla.org/en-US/docs/JavaScript/Reference/… here u can read about it Commented Sep 12, 2012 at 11:09
  • Specification: Property AccessorsEvaluatePropertyAccessWithExpressionKeyToPropertyKey. A property key is obtained by either keeping the symbol value or coercing the value to a string. Commented Jun 8, 2023 at 1:52

2 Answers 2

13

What does it mean to use an object as a key to a property in JavaScript?

Javascript objects only allow string keys, so your object will first be coerced to a string.

Specifically, why does using looking up b in c return the value that was stored in a property of a?

The string representation of {a: 1} and {b: 2} are both "[object Object]", thus, the property is overwritten.

Edit: If you really need to use objects as keys (I would prefer another solution, if possible), you could use the object's JSON representation:

c[JSON.stringify(a)] = 1
c[JSON.stringify(b)] = 2

But, again, try to think of a different approach. Perhaps the objects have unique identifiers other than the object itself.

Sign up to request clarification or add additional context in comments.

1 Comment

Cheers Linus, this makes good sense. I do need to use objects as keys in a sense, so an actual Map class would be useful. However I won't have very many in my set, so I think I'll just create an array of key/value pairs, and on 'get' loop through them, testing keys with ===. It'll be O(N), but my N will be around 10 so I'm not bothered. Thanks again.
2

Why you use an object as a key, the key is becoming the object.toString() 's result which is [Object Object],

So what you are dothing is set a value to the property "[Object Object]", and get the value by the property "[Object Object]".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.