Question: I know what the following code DOES, I'm just unable to wrap my head around the WHY.

```
var myObject = new Object();
var foo = new Object();
var bar = new Object();

myObject[foo] = 'foo';
myObject[bar] = 'bar';
console.log(myObject[foo]);  // returns bar
console.log(myObject); // returns [object Object]: "bar"
console.log(myObject[blah]); // returns "blah not defined" error.

```

By this measure, how is myObject[blah] undefined yet myObject[foo] and myObject[bar] *are* defined - and not only defined, but set to the same value?

According to <http://www.w3schools.com/js/js_objects.asp> an object's properties can be accessed via `object.property` **or** `object[property]` however if I add in `myObject.foo = "foobar";` before the console logs, `myObject[foo]` does not get set to "foobar" but `myObject.foo` does.

If I console.log `myObject.Object` or `myObject.object` or `myObject[object]` or `myObject[Object]` - it all comes back as undefined.

Both foo and bar have values of `Object {}`. So they aren't being set.

Answer: 
The problem is with types here.

`foo` is an object, you can't access a property of an object with another object as a key.

Although `foo` will be converted to a string and no error will occur, be aware that what will finally be evaluated is this:

```
myObject[foo] = 'foo';
myObject[bar] = 'bar';

```

Translated to

```
myObject["[object Object]"] = 'foo';
myObject["[object Object]"] = 'bar';

```

> 
> By this measure, how is myObject[blah] undefined yet myObject[foo] and
>  myObject[bar] are defined - and not only defined, but set to the same
>  value?
> 
> 
> 

This message error does not tell you that the key does not exist, as it would not be an error. What it tells you instead is that `blah` is a variable you are trying to use whereas you never defined it before, thus throwing an undefined reference error.

---

Also, unrelated but `{}` syntax is always preferred to `new Object()`
You're referencing the variable `blah` which is not defined.

`foo` and `bar` are defined but it's very strange to use them as keys in another object. JavaScript doesn't support HashMaps by default, instead it will perform a to-string cast, this produces `"[object Object]"`.

So `myObject[foo]` and `myObject[bar]` are both just `myObject["[object Object]"]`

Did you intend `myObject["foo"]` and `myObject["bar"]`"?

The syntax is either:

```
obj["property"];
// or
obj.property;
// or
var prop = "property";
obj[prop];

```