Live data from Hacker News

How Good C# Habits can Encourage Bad JavaScript Habits

enterprisejquery.com

1–10 of 17 posts

Re: How Good C# Habits can Encourage Bad JavaScript Habits

#4
post #3

Forgive me, but it's still not clear (in terms of the outcome) to me what the difference is between: var a = new Array(); and var a = []; Would someone shed some light on this?

The outcome of those two is the same. Likewise the outcomes of

  var a = new Array(3, 4);
and

  var a = [3, 4];
are the same.

However,

  var a = new Array(3);
is not the same as

  var a = [3];
The former creates an array with 3 empty elements, the latter an array with a single element initialised to 3. So the constructor syntax is dangerous to use with just one argument. Plus, the literal is just easier to type and spot visually.

There is a slight performance penalty for using named constructors: the runtime needs to look up the "Array" entry in the scope; nothing (except common sense) stops you from redefining it or shadowing it with a local variable. The literal syntax can be resolved at parse/compile time.

Fun JS array fact: the implicit arguments argument in each function looks like an array but its prototype is actually not Array. None of the Array methods work on it, although the special length property behaves as you'd expect.

Re: How Good C# Habits can Encourage Bad JavaScript Habits

#5
post #2

Good C#|Java|Python habits == good MooTools habits

I found the C# link in the article to be tenuous at best. JavaScript doesn't have namespaces or classes, so yes, obviously you'll need to do without them. Global variables are discouraged in C# - in contrast, uh, avoid global variables in JavaScript? Likewise, I wouldn't call creating objects using a constructor a "best practice" if that's the only way to do it in the language. Besides, there is a time and place for using new, even in JavaScript, mainly for creating functionality-heavy objects with a prototype. (as opposed to data containers)

Re: How Good C# Habits can Encourage Bad JavaScript Habits

#6
I don't quite agree with everything the author said. For example, the new keyword can make the code a lot more readable if you use it to call your own constructors (that is, functions that assume that "this" is referencing a newly created object).

Also, if you're dealing with DOM, you might want to avoid anonymous functions and closures as IE tends to not do proper garbage collection within closures and leak an awful lot of memory that doesn't get freed even after you visited another page. (This is an oversimplification, Google "IE DOM memory leaks" for a better description of the issue.) This is not JavaScript's fault, but still it is an important thing to consider as your users will hate you if your site is crashing their browser even if it's not your fault.

Re: How Good C# Habits can Encourage Bad JavaScript Habits

#7
I don't see why a person with a C# background would have any of these (2) "bad habbits". On the contrary, someone with some C# experience would do his best to avoid global variables, and one of the first things he would search for would probably be how to do namespacing with javascript. The new keyword may be an issue at the beginning but as soon as you do some JSON, you automatically get the correct way of declaring arrays and objects.

Re: How Good C# Habits can Encourage Bad JavaScript Habits

#8
post #4
post #3

Forgive me, but it's still not clear (in terms of the outcome) to me what the difference is between: var a = new Array(); and var a = []; Would someone shed some light on this?

The outcome of those two is the same. Likewise the outcomes of var a = new Array(3, 4); and var a = [3, 4]; are the same. However, var a = new Array(3); is not the same as var a = [3]; The former creates an array with 3 empty elements, the latter an array with a single element initialised to 3. So the constructor syntax is dangerous to use with just one argument. Plus, the literal is just easier to type and spot visu…

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

#9
post #8
post #4

Earlier quoted context omitted.

The outcome of those two is the same. Likewise the outcomes of var a = new Array(3, 4); and var a = [3, 4]; are the same. However, var a = new Array(3); is not the same as var a = [3]; The former creates an array with 3 empty elements, the latter an array with a single element initialised to 3. So the constructor syntax is dangerous to use with just one argument. Plus, the literal is just easier to type and spot visu…

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?

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

#10
post #8
post #4

Earlier quoted context omitted.

The outcome of those two is the same. Likewise the outcomes of var a = new Array(3, 4); and var a = [3, 4]; are the same. However, var a = new Array(3); is not the same as var a = [3]; The former creates an array with 3 empty elements, the latter an array with a single element initialised to 3. So the constructor syntax is dangerous to use with just one argument. Plus, the literal is just easier to type and spot visu…

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?

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.
Post reply on HN