Reminds me of one of the more confusing bugs I've ever encountered in my life. I was throwing together a quick UI with Adobe Flex, and for some reason every time you clicked a particular button, the entire UI would shift 20 or so pixels to the right. I spent hours scratching my head until I noticed this for loop: for (x=0;x I wasn't declaring the x variable, so it was using the x part of the x/y positioning of the UI…
I wasn't initialising the x variable Do you mean s/initialising/declaring/, or am I just really confused?
How One Missing `var` Ruined our Launch
41–50 of 223 posts
Re: How One Missing `var` Ruined our Launch
#42> I would posit here that nothing I could do in best practice (manual front-end testing, unit testing, error handling, etc.) would have caught the offending line. jshint would have caught it. You need to run jshint on your code or you will get silly errors like this. Simple.
Re: How One Missing `var` Ruined our Launch
#43Perfect example of a developer hating JS just because he/she didn't bother to learn it first and got burned.
Re: How One Missing `var` Ruined our Launch
#44Reminds me of one of the more confusing bugs I've ever encountered in my life. I was throwing together a quick UI with Adobe Flex, and for some reason every time you clicked a particular button, the entire UI would shift 20 or so pixels to the right. I spent hours scratching my head until I noticed this for loop: for (x=0;x I wasn't declaring the x variable, so it was using the x part of the x/y positioning of the UI…
I wasn't initialising the x variable Do you mean s/initialising/declaring/, or am I just really confused?
Re: How One Missing `var` Ruined our Launch
#45If so, that is an insane design.
Re: How One Missing `var` Ruined our Launch
#46Earlier quoted context omitted.
Even better is setting your editor to run JSHint when you save a .js file, and let you know if there are problems. Not only does it avoid stupid bugs, it saves time round-tripping to the browser for trivial issues like syntax errors.
Sounds like a good idea. I know emacs flymake mode can be set up to underline problems detected by jshint. Personally I like to have the tests and jshint run by a hotkey so I can happily move the code through invalid states (towards a valid goal) without being constantly complained at :)
Re: How One Missing `var` Ruined our Launch
#47Since everyone is chiming in with ways to prevent this sort of thing, here is another: js2 mode for Emacs[1]. This is a mode originally written by Steve Yegge and then modified by some other people (be sure to get that version) that actually parses the code and, among other things, highlights global variables in a different color than local ones. I find this, along with the other things js2 does, helps prevent a whol…
Have you had any luck getting it to handle modern JavaScript style? Whenever I've tried js2-mode it really hasn't liked jQuery style nested anonymous functions.
I've done some moderately complicated jQuery development with it, as well as some node.js stuff for fun, and have had no issues in either case.
Re: How One Missing `var` Ruined our Launch
#48Scheme got it right sometime in the 1970's.
(let ((x initial-value)) ; binding
(set! x new-value)) ; assignment
Or in infix syntax (Dylan): let x = initial-value ;
x := new-value ;Re: How One Missing `var` Ruined our Launch
#49Am I right in thinking that the javascript /* "use strict" */ construct would have caught this mistake (just like it would have done for me in perl code)? Seems to me that some such feature is absolutely and utterly necessary in any environment where you're doing a lot of closure creation ..
Maybe Node should be strict by default? I can understand it not being the case for browsers that need to support legacy code, but for Node it doesn't really need to care about that.
Re: How One Missing `var` Ruined our Launch
#50Do I understand this right -- global variables are shared across all requests in node.js? If so, that is an insane design.
Happily, this is all changing--in the long run, future versions of JavaScript should polish away these issues while maintaining the fundamentally sound core of the language. In the short run "use strict" lets you realize some of the upcoming improvements in otherwise legacy code.