Hacker News new | past | comments | ask | show | jobs | submit login
Goodbye “undefined is not a function” (plus.google.com)
522 points by tosh on Feb 22, 2015 | hide | past | favorite | 65 comments



This change required adding a pretty-printer into V8 to massage that AST back to something recognizable: 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 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.


SpiderMonkey has something that we call ExpressionDecompiler [1]. Basically it allows us to decompile some subset of the JS bytecode. Fun fact: At some point in time SpiderMonkey could actually decompile all bytecode and this is how we used to generate the result of Function#toString. [2]

[1] https://dxr.mozilla.org/mozilla-central/search?q=ExpressionD... [2] https://bugzilla.mozilla.org/show_bug.cgi?id=718969


It doesn't seem relevant to the bug you posted, but as long as you're here... I'm always annoyed by the vagueness of JSON.parse syntax errors. Instead of showing the source file and line number of the call to json.parse (or even better the location of the syntax error in the json string it failed to parse) I get "uncaught syntax error in index.html:1". I know now that that usually means i've forgotten to wrap a json.parse in a try..catch, but it's caught me out a few times.


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.


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.


Hey Paul great work! Any idea on when the promises support in the dev tools is landing?


Can it throw an exception when we're using a js library that isn't cool anymore?


Exceptions as a service.js


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?


I'd just point to C++'s error output: http://www.codeproject.com/KB/cpp/791461/errors_temp.PNG


All I see is blonde, brunette and redhead


Reference for those confused: https://www.youtube.com/watch?v=3vAnuBtyEYE


I can't leave my socks on the floor anymore because Roomba will eat them.


Bug or feature?


That just means I lock my room's door and it never gets cleaned. I wish it would just make me not put my socks on the floor.


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


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.


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.


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

  foo = {}
  foo.bar()
Or, more commonly,

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


Not just clearer, transmits more information!


Even better: "Error, you are using JavaScript."


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


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?


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


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.


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.


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.


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.



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.


Why can't you use source maps in production?


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


You usually step through the code you were last working on and it's in there.


Use a query string to run an unminified version in prod.


I just use https://getsentry.com/. Seems to work pretty well, both across browsers and with my minified code.


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


I would like to add fuel on this javascript-hate fire: https://www.destroyallsoftware.com/talks/wat


You should watch his other videos.


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


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


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


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? :)


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


It's available in Chrome Canary.


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


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.


Well, for one, the person that posted this link has 9-10 times more karma than you: karma: 4659

I'm a bit cynical, but I definitely think that had something to do with it if it wasn't just for the timing.


I never visit links to Twitter.


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.


Will this work with source maps?


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.


[deleted]


TypeError: (intermediate value)(...) is not a function

(in firefox :^)


What version of FF are you on? I'm seeing this in FF 35.0.1:

    TypeError: foo.bar is not a function


TypeError: undefined is not a function (evaluating 'foo.bar()')

(for completeness, in Safari)


  Internet Explorer has stopped working

  Windows is checking for a solution to the problem...

  [||||||||||||||||||||||||||||||||||||||||||||||||||]

                                            [ Cancel ]


Uncaught TypeError: (intermediate value)(intermediate value)(...) is not a function

chrome canary


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.


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.


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


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


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



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


2015.


YESSSSŚŠSSSSSS




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: