
Goodbye “undefined is not a function” - tosh
https://plus.google.com/+AddyOsmani/posts/DdWkiKsvbA2
======
paulirish
This change required adding a pretty-printer into V8 to massage that AST back
to something recognizable:
[https://codereview.chromium.org/861623002](https://codereview.chromium.org/861623002)

Still, there are decent improvements we could make. Star, or better yet,
comment on
[https://code.google.com/p/chromium/issues/detail?id=259443](https://code.google.com/p/chromium/issues/detail?id=259443)
with how you get caught out by weak error messages. We want the debugging
cycle to be a lot more direct from error to fix.

~~~
po
Thank you. This will save so much development time globally.

Please also look into the "Script error" message. I know it is intentionally
non-descript to prevent leaking information about cross domain requests but
there has to be something that is safe to report that will help developers
narrow down what is causing it.

~~~
jon-wood
Given developers can usually narrow it down after a bit of trial and error I
see no reason to obfuscate this. By its nature all the source is available to
anyone asking, so let's just stop making CORS debugging such a pain.

------
drcode
First, I get a roomba, and now this...

For those of you who argue that the US standard of living hasn't improved
since the 70s: How do you defend your position in the face of this
overwhelming counter-evidence?

~~~
UnoriginalGuy
I'd just point to C++'s error output:
[http://www.codeproject.com/KB/cpp/791461/errors_temp.PNG](http://www.codeproject.com/KB/cpp/791461/errors_temp.PNG)

~~~
shultays
All I see is blonde, brunette and redhead

~~~
rgawdzik
Reference for those confused:
[https://www.youtube.com/watch?v=3vAnuBtyEYE](https://www.youtube.com/watch?v=3vAnuBtyEYE)

------
Zarel
I think "foo.bar is undefined, not a function" might be clearer.

~~~
kevan
This would help a lot in case where you have multiple calls chained together.

    
    
        foo.bar().fiz().buz()
    

What's undefined, bar, fiz, or buz? Then you have to set a breakpoint to find
out. Most people I know try to avoid writing code like this, but Knockout
ViewModels are a common place where this bites me.

~~~
eyko
If bar() or fiz() return undefined (i.e. don't return at all), then you will
still get the same error as usual undefined is not an object, I reckon.

~~~
saraid216
The issue isn't that the functions are returning undefined. It's...

    
    
      foo = {}
      foo.bar()
    

Or, more commonly,

    
    
      foo = {};
      bar = 'bar';
      foo[bar]();

------
rtfeldman
This is cool, although the title reminded me of one of my favorite parts of
Elm[0]...I've effectively said goodbye to “undefined is not a function”
because I've said goodbye to runtime exceptions in general.

More specifically, I have yet to write Elm code that compiles and throws a
runtime exception. I sure don't miss seeing "undefined is not a function!"

A top-shelf compiler is a hell of a drug. :)

[0] [http://elm-lang.org](http://elm-lang.org)

~~~
millstone
How does Elm handle browser differences? For example,
window.clipboardData.setData is only defined in IE, so what happens if I call
it unconditionally from Elm, and am using Firefox?

~~~
rtfeldman
Depends on what you're doing. In some cases Elm.Native will take care of
browser differences.

In general, Elm's replacement for the concept of undefined is Maybe, and the
compiler will tell you if you forgot to handle something properly:

[http://elm-lang.org/edit/examples/Functional/Maybe.elm](http://elm-
lang.org/edit/examples/Functional/Maybe.elm)

~~~
millstone
After some research, it looks like my answer is that I can't call it
unconditionally from Elm. Elm doesn't provide an easy way to access arbitrary
JavaScript properties, and doesn't attempt to expose all of the functionality
of the browser, but instead provides its own API.

------
DigitalSea
This has been one of the biggest annoyances of debugging Javascript I can
think of. Maybe it is rather small to many, but this is a big deal for me and
I cannot wait for it to land in the stable version of Chrome (even though I am
a Firefox user), I still test in Chrome.

------
jordanthoms
This is one of my pet peeves with most languages' error messages - e.g in
Ruby: 2.2.0 :001 > a = nil; b = " "; c = "a"; 2.2.0 :002 > a + b + c
NoMethodError: undefined method `+' for nil:NilClass from (irb):2

It would be trivial to tell me what the name of the nil variable was - in this
case it's obvious, but it's often not.

~~~
adrusi
Except in Ruby, that error is raised by `NilClass#method_missing`.
`method_missing` gets to know the name of the method being called, and the
arguments passed to it, but it deals with the value, not the variable. It
wouldn't be "trivial" to report the name of the variable, it would probably
involve intercepting all NoMethodErrors raised by NilClass and then dropping
into the debugger to know where exactly execution stopped. I'm not sure this
is even possible with the current architecture of the Ruby interpreter.

~~~
nilved
[http://ruby-doc.org//core-1.8.7/Kernel.html#method-i-caller](http://ruby-
doc.org//core-1.8.7/Kernel.html#method-i-caller)

[https://github.com/banister/method_source](https://github.com/banister/method_source)

------
jfaucett
This is awesome! If there's one area that still needs a lot of work in the js
world I think its exception messages. Its hard to believe its 2015 and its
still so hard get clean stack traces for your minified js in production since
you can't source map it there.

I wonder how large js apps handle production debugging and error reporting on
all browsers? The last time I tried window.onerror it had decent support but
not comprehensive support across browsers. Maybe its just me but this seems
like a problem really needing to be solved, especially as js apps get bigger
and bigger.

~~~
TheAceOfHearts
Why can't you use source maps in production?

~~~
pimlottc
OP may be referring to jQuery's removal of the sourcemap comment last year,
starting from 1.11/2.1 [0]. Basically, many sites deploy the minified library
without the sourcemap, leading to tons of 404 errors which confuse users.

0: [http://bugs.jquery.com/ticket/14415](http://bugs.jquery.com/ticket/14415)

------
codewithcheese
Happy to see someone is working on improving js error messages. This is a good
example of why people hate Javascript.

~~~
Fiahil
I would like to add fuel on this javascript-hate fire:
[https://www.destroyallsoftware.com/talks/wat](https://www.destroyallsoftware.com/talks/wat)

~~~
saraid216
You should watch his other videos.

------
evincarofautumn
I am certain that someone somewhere is relying on the old exception message,
and that this change broke their code.

~~~
acjohnson55
They'll either be ready or they'll learn a valuable lesson.

------
viggity
If you want something similar for .net, please consider upvoting this issue
now that it is open source
[https://github.com/dotnet/coreclr/issues/25](https://github.com/dotnet/coreclr/issues/25)

------
sandstrom
This is a great adjustment.

Another one (which IE may have stopped doing in recent versions) is the
annoying translation of exceptions.

    
    
        O servidor Automation não consegue criar o objeto -- anyone? :)

~~~
ygra
If you hate other languages so much, why not use English as your UI language,
then?

------
tosh
It's available in Chrome Canary.

------
inglor
Not going to hijack the thread or anything - I'm just wondering why when I
posted it yesterday it went relatively unnoticed?

As someone who might have to do some startup marketting - what did I do wrong
from a copyrighting point of view?

Here is the post
[https://news.ycombinator.com/item?id=9087763](https://news.ycombinator.com/item?id=9087763)

~~~
chrisheller
I saw the original link on my phone yesterday. The screenshots in the post
were unreadable since pinch and zoom was disabled, so I didn't fully
understand until pulling up HN on a larger screen today.

I have no idea how many other people might have read your submission on their
phone and had the same issue though.

------
akennberg
Can we get a properly working window.onerror? Currently we either get a very
generic Script Error (0 information for debugging) or have to wrap every
native method in one with a try/catch. This is more for error log collection
and aggregation to understand what's breaking in production.

------
dkersten
Will this work with source maps?

~~~
skybrian
Someone should try it, but it seems unlikely if V8 is generating the error
message. Dev Tools loads the source maps and as far as I know, V8 doesn't know
about them.

------
bsimpson
Certainly a great improvement, but it's sad that the WebKit Dev Tools are well
over a decade old, and this change is just coming now. It shows how much room
there is for improvement in developer ergonomics.

------
bpg_92
This is awesome! "undefined is not a function" was one big problem for me when
I started cleaning up code, and didn't know what caused the issue, I spend a
lot of time debugging.

------
criloz2
Wow! thanks.. my hate towards JavaScript has been reduced dramatically with
this update.

~~~
bzbarsky
Javascript, or V8 and Chrome's devtools? Firefox has had sane error reporting
in this situation for quite a long while....

------
riffraff
while this is awesome, doesn't it affect compatibility? i.e. if a library was
checking the error message

~~~
Gurkenmaster
[https://github.com/mattdiamond/fuckitjs](https://github.com/mattdiamond/fuckitjs)

------
martinko
If we're lucky, this might make it into node by 2020!

------
CmonDev
2015.

------
aerovistae
YESSSSŚŠSSSSSS

