Getting a property of undefined
What is the code triggering the message?
let z = { a: { b: { } } }
z.a.b.c.d
What went wrong in the code? What is the system trying to tell you?
That z.a.b.c is undefined, so you can't access the d property on it
What error message do you see? Which JavaScript environment produced this error?
#### jsc
TypeError: undefined is not an object (evaluating 'z.a.b.c.d')
#### V8
TypeError: Cannot read property 'd' of undefined
#### chakracore
TypeError: Unable to get property 'd' of undefined or null reference
#### spidermonkey
TypeError: z.a.b.c is undefined
eshost shows that only SpiderMonkey gives helpful output
What error message would you like to see?
SpiderMonkey-style output in all JS engines
Do you agree to license your suggested error message under an MIT-style license?
Yes
Playing around with elm style errors again.. just to see what they might look like:
TypeError: z.a.b.c is undefined.
--> components/some.js:511:26
511 | z.a.b.c.d
| ^^^
| |
| Property 'c' is undefined. Cannot access property 'd'.
Another example related to this...
class Calculator {
constructor() {
this.value = 0;
}
add(number) {
this.value += number;
}
subtract(number) {
this.value -= number;
return this;
}
};
let cal = new Calculator();
cal.subtract(3).add(5).add(2);
This code, which forgets to return this from the add method, gives you the following error:
TypeError: Cannot read property 'add' of undefined
I really like the Elm style pointers that tell you exactly there something is undefined, but in this case, when a method is returning undefined, it would be nice to be even more explicit and say something like The add() method of cal is returning undefined.
I know that's probably a long shot, but A GIRL CAN DREAM.
I feel like the spidermonkey message is incomplete. I read that 4 times before I realized it was only talking about 'c' instead of 'd'. While it's correctly identifying the root problem, it doesn't mention the symptom at all and that seems surprising.
I suggest:
"TypeError: z.a.b.c.d could not be evaluated because z.a.b.c is undefined."