Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I see two things in lots of JavaScript these days, both of which probably contribute to the perception of JavaScript as a bad language:

1. Anonymous functions assigned to variables. These make debugging a nightmare. Some (possibly all) versions of IE reports something like "error on line 0" when it occurs in such a function, so you really have no idea what went wrong.

2. Accidental closure creation.

Both of these are avoided by just declaring your functions statically. If you want to create an object, you can still assign a static function to a property, e.g.:

  function foo() { ... }
  Foo.prototype.foo = foo;
Back when I used to primarily write JavaScript, and compatibility with things like Netscape and IE3 were a concern, I'd write all my code roughly like that and just namespace the functions if they are for a particular class, e.g.:

  function className_functionName() { ... }
  className.prototype.functionName = className_functionName;
This guarantees maximum compatibility with very old versions of JavaScript/JScript, runtime efficiency (functions get evaluated at parse time not runtime, also guaranteeing only one copy of the function code is ever created) and no possibility of accidentally creating a closure where you don't want to (which can lead to memory leaks).

The downside is that it it makes your JavaScript code look more like C++ code that Java, and even less like Ruby. It also requires a tiny bit more typing, and we all know how much programmers hate typing.



The problems you describe are due to bad runtime environments rather than problems of the language.

Don't write JS in that style when compatibility with ancient browsers is not an issue. Hard to read, maintain and understand.




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

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

Search: