News for hackers. Stuff that matters.
Re:How Good C# Habits can Encourage Bad JavaScript Habits by scott_s on Thursday October 07, 2010 @05:20PM (#1769586)
The former creates an array with 3 empty elements, the latter an array with a single element initialised to 3.

Ugh. I can see that catching lots of people. Is new part of the language, or part of jQuery?

Re:How Good C# Habits can Encourage Bad JavaScript Habits by pmjordan on Thursday October 07, 2010 @05:37PM (#1769645)
new is a keyword and has various other gotchas. The most egregious is probably is the silent but deadly failure when you forget it altogether:

  function MyURL(loc) {
    this.location = loc;
  }
  
  // benign:
  var url1 = new MyURL("http://news.ycombinator.com");
  
  // redirects the user to Hacker News:
  var url2 = MyURL("http://news.ycombinator.com");
Calling a free function binds the global object (window in the browser) to the implicit this parameter; assigning a URL to window.location is clearly quite an obvious bug, most cases of forgetting new when calling a constructor are much more subtle. Luckily, you can defend against it in the constructor's code:

  function MyURL(loc) {
    if (this === (function(){return this;})())
    {
      // might want to log this during development, too...
      return new MyURL(loc);
    }
    this.location = loc;
  }
I strongly recommend reading what Douglas Crockford has written on the topic of JavaScript, or watching his excellent videos. (this from someone who detests video as a medium of conveying this sort of info)
Re:How Good C# Habits can Encourage Bad JavaScript Habits by user24 on Thursday October 07, 2010 @07:34PM (#1769961)
pmjordon has a great reply, but I'd like to add that I'm a little surprised by your mention of jQuery... jQuery is just a framework built on top of JavaScript; it's not like it's a different language or even a language extension. It's just a standard piece of JavaScript code, an object which you can use.
Re:How Good C# Habits can Encourage Bad JavaScript Habits by scott_s on Thursday October 07, 2010 @09:15PM (#1770206)
I am unfamiliar with the details of JavaScript's semantics, so I won't be able to tell at a glance what is part of the language and what is part of a library.