4 ms·
In JavaScript I often find my self running the code in both firefox and chrome to get the full picture of what is undefined: (firefox) window.foo.bar /
by runarberg 8y ago
In JavaScript I often find my self running the code in both firefox and chrome to get the full picture of what is undefined: (firefox)
window.foo.bar
//=> TypeError: window.foo is undefined
and what I'm trying to get: (chrome)
window.foo.bar
//=> Uncaught TypeError: Cannot read property 'bar' of undefined
Personally I find the firefox error message more useful, but often I get a better understanding of whats wrong when I run it in chrome. I don’t know why they are mentioning undefined at all and don’t say something like:
TypeError: Cannot read property 'bar' of 'window.foo' (undefined)
---
Edit: Formatting
- zamadatix 8y agoOn Chrome if you click the hyperlink on the far right side of the error it brings you to the line that caused the error. Not as easy as if it were displayed as part of the error but easier than switching between browsers.
- ashelmire 8y agoThis helps but isn't always sufficient, for example in conditionals where comparing property x of two different objects. I like the solution proposed here.
- bzbarsky 8y agoThe worst part is that Firefox tried to change the error message it throws to include the "bar" piece of information, and that had to be reverted because it broke sites that were parsing the exception message with regexps. :( See https://bugzilla.mozilla.org/show_bug.cgi?id=1498257 https://bugzilla.mozilla.org/show_bug.cgi?id=1498257 for the gory details, though it was not the only site affected: see also https://bugzilla.mozilla.org/show_bug.cgi?id=1490772 https://bugzilla.mozilla.org/show_bug.cgi?id=1490772 (fixed by site author) and https://bugzilla.mozilla.org/show_bug.cgi?id=1512401 https://bugzilla.mozilla.org/show_bug.cgi?id=1512401 (fixed by the backout).
- gpm 8y agoAnyone know what the future of this is? Sounds like they were considering trying to get sites to fix this and then redo the change, but it's not clear what happened to that in the last 5 months.
- bzbarsky 8y agoSee most recent comments in the bug. The short answer is probably "there is no future here".
- gpm 8y agoYou're referring to https://bugzilla.mozilla.org/show_bug.cgi?id=1498257 https://bugzilla.mozilla.org/show_bug.cgi?id=1498257 right? I read all the comments, didn't get that impression, can you say why you do?
- artursapek 8y agoSeems to me that a developer relying on behavior like that should be left to fend for themselves. It's not like the behavior is already consistent between implementations of ES5, or a part of its spec.
- bzbarsky 8y agoIn this case "left to fend for themselves" manifests as "users end up with a non-working (on some sites) browser, switch browsers". So it's not really an option, unless the browser involved is a monopoly and the site involved has a tiny audience.
- tarabanga 8y agoTypeError: Cannot read property 'bar' of undefined 'window.foo'