Live data from Hacker News

How One Missing `var` Ruined our Launch

blog.meloncard.com

211–220 of 223 posts

Re: How One Missing `var` Ruined our Launch

#211

Steve Yeggae's js2-mode highlights globals during editing, and it's saved me a good many times. http://code.google.com/p/js2-mode/ Mind you it won't catch globals declared in chained assignments eg var x = y = 0; // y is defined globally

The fork of js2-mode at https://github.com/mooz/js2-mode will catch that, as will js3-mode.

Re: How One Missing `var` Ruined our Launch

#212

Actually it is very easy to debug any javascript error. In IE there is a setting where you can uncheck the Disable Script debugging (Internet Explorer) and in the status bar you will see any javascript error. If you check the checkbox IE will not report any errors. In general during your development you should always uncheck the box.If you double click the javascript status this will exactly report the line number wh…

The funny thing about this comment is that you think he can use Internet Explorer to check his server-side code.

Re: How One Missing `var` Ruined our Launch

#213

I know there seem to be a few HN readers religiously against using an IDE, but in this case an IDE would have probably helped. Since it colors global and local variables differently the error would be much more apparent at the time when it was made (you typically have very few global variables so they stand out color wise).

No IDE needed - any decent editor will do syntax highlighting.

Re: How One Missing `var` Ruined our Launch

#214

I know there seem to be a few HN readers religiously against using an IDE, but in this case an IDE would have probably helped. Since it colors global and local variables differently the error would be much more apparent at the time when it was made (you typically have very few global variables so they stand out color wise).

No IDE needed - any decent editor will do syntax highlighting.

Global versus local variables is not determined by syntax in javascript, it's determined by semantics (you need to parse javascript to find out). A few editors can do that, but you could really call those an IDE already.

Re: How One Missing `var` Ruined our Launch

#215
post #5

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…

Now that I understand what you're saying: This is one of the reasons I hate the "feature" in C++ and C99 that you can declare variables anywhere. If you stick to declaring all your variables in one place, it's much easier to notice when you've already used a variable name.

That makes variables scope over far more than they need to, opening up a pretty frequent bug avenue of using the wrong variable that you did not intend to use.

Re: How One Missing `var` Ruined our Launch

#216
post #182

This is why I really dislike it when I see var a = "foo", b = "bar", c = "baz"; All it takes is one missed comma and all of a sudden you've turned a lot of your variables global.

that's why many people favor this variant: var a = "foo" , b = "bar" , c = "baz" ;

That makes the commas easier to see (and is ugly IMO) but it's still one comma away from making the rest of your variables global. What's so bad about just

    var a = "foo";
    var b = "bar";
    var c = "baz";

Re: How One Missing `var` Ruined our Launch

#218
post #159
post #58

Earlier quoted context omitted.

Now there's an argument for syntax highlighting. It also doesn't happen in languages where you must say self.x to access a property/field called x .

Exactly my first thought: "This wouldn't happen with Python".

Only for case of instance variable. Python still has nested function, non-instance variable, and implicit declaration.

Only it is worse in Python because, lacking `var` keyword, you will shadow the variable in Python

Re: How One Missing `var` Ruined our Launch

#219

    > (function () { internalvariablewithnovardelcaration = "string"; })();
    > internalvariablewithnovardeclaration
    ReferenceError: internalvariablewithnovardeclaration is not defined
        at [object Context]:1:1
        at Interface. (repl.js:179:22)
        at Interface.emit (events.js:64:17)
        at Interface._onLine (readline.js:153:10)
        at Interface._line (readline.js:408:8)
        at Interface._ttyWrite (readline.js:585:14)
        at ReadStream. (readline.js:73:12)
        at ReadStream.emit (events.js:81:20)
        at ReadStream._emitKey (tty_posix.js:307:10)
        at ReadStream.onData (tty_posix.js:70:12)
    > externalwithnovar = 1;
    1
    > (function () { externalwithnovar = "string"; })();
    > externalwithnovar
    'string'
You're all wrong. There is no 'global by default for javascript. This guy clobbered a variable called initial outside of the scope of the callback.

Re: How One Missing `var` Ruined our Launch

#220
post #3

Am 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 ..

[deleted]
Post reply on HN