How Good C# Habits can Encourage Bad JavaScript Habits
enterprisejquery.com
enterprisejquery.com
Similarly, the global namespacing - a C# developer is going to look for ways to declare namespace.definition style, and is not going to spontaneously come up with namespace_definition, as this is an unseen pattern in C# examples and general practice.
Perhaps it would be a good primer for a VB6 -> Javascript developer, but I don't imagine there are too many of those around.
Why not? Some of us are (were) stuck maintaining bad VB6 apps and asp webpages with vbscript that we had to migrate to javascript as Firefox doesn't support vbscript.
I no longer do this, but it paid above average for my country and I was starting out as a developer. And yes, my code probably did suck (heck, it probably still does).
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.
To the best of my knowledge this was fixed in IE 7. Therefore I consider having such leaks on the general internet to be an underhanded service. Anything that leaves IE 6 usable but less pleasant encourages people to migrate to something better. ;-)
" However, as some web developers have pointed out, those changes don’t solve the problem entirely. IE still leaves behind anything not attached to the tree when we tear down the markup. In addition, sites that users keep open for extended periods of time, such as Web-based mail, can still cause IE’s memory usage to continually grow if the site doesn’t take care to avoid the leak patterns."
http://blogs.msdn.com/b/ie/archive/2007/11/29/tools-for-dete...
It's still a major pain in the back.
var a = new Array(); and var a = [];
Would someone shed some light on this?
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.
Ugh. I can see that catching lots of people. Is new part of the language, or part of jQuery?
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) arguments = Array.prototype.slice.call(arguments);