Why JavaScript is my favourite language
I tell everyone, JavaScript is my all-time favourite programming language and they look at me as if I have some terminal illness.
“No!” I tell them, honestly! Well, not really. ECMAScript is my favourite language, which is the reference language syntax for JavaScript (Mozilla) and JScript (Microshit).
You see, the big problem with JS that web developers run in to is not that JavaScript sucks, but the implementation sucks. As a language, ECMAScript is bloody awesome. Specifically, Microsoft’s implementation is fucking awful but Mozilla is not without it’s problems. Macrodobe’s is very good (Flash) and there are a couple of others that are alright.
So why do I like JavaScript so much? you ask – well, look at this snippet. I’m building a table from a dataset (in one statement!):
table = Builder.node("table", {"class":"kd_information"}, [ Builder.node("caption", "Text information"), Builder.node("tbody", [ Builder.node("tr", [ Builder.node("th", "Title:"), Builder.node("td", data.title) ]), Builder.node("tr", [ Builder.node("th", "There was lots more boring stuff here that I have snipped:"), Builder.node("td", data.tagdata.headings) ]), Builder.node("tr", [ Builder.node("th", "Linked external domains:"), Builder.node("td", [ Builder.node("ul", function() { l = [] data.extdomains.each(function(domain) { if (domain) l.push(Builder.node("li", [ Builder.node("a", {"href":"http://"+domain+"/"}, domain) ])) }) return l }()) // Look at that little beauty ]) ]) ]) ])
You’re looking at lines 15-24. I needed to make a ul/li list from a list in memory. But oh no, I’m in a statement, what do I do! Finish up, find the UL node then iterate my list appending LIs to it? FUCK THAT! Define an inline function that returns the list and immediately call it
Bloody awesome.
Update:
Here’s another little example for you. Lines 10-15:
new Ajax.Request("/clickability.py/get_prmap", { method:"post", postBody:"uniqid="+escape(GWTBL.uniqid), onSuccess: function(t) { data = eval('('+t.responseText+')') data.each(function(info) { width = 400 lw = Math.round(width * info.fraction) rw = width - lw tbody.appendChild(Builder.node("tr", [ Builder.node("td", function(pr) { if (pr == null) return "Not checked" if (pr == -1) return "Not available" return "PR "+pr }(info.pagerank)), // Catch that funky syntax, wide-boi Builder.node("td", "Some more boring stuff") ])) }) } })
-Rob





Martin 2255 days ago
http://www.saulis.comhey Dave. why don’t you use jQuery for this eh?
creativesuit 2255 days ago
http://www.creativesuit.co.ukI do admire your ability to swear through every post Dave.
There’s not enough swearing in SEO Blogs.
Jack 2255 days ago
http://www.digitalbricklayer.com/blog/Javascript great til you get into the OOP bits….Euw minging!
James 2255 days ago
http://www.jamesbooker.co.ukActually, I believe this is the most comprehendible post I’ve ever seen Dave write. I lurk here and read all the posts, but sometimes find it hard to follow Dave’s post – especially since his RSS feed looks so crap in Google Reader.
More like this please, Mr N
Rob Haswell 2255 days ago
Hah Dave didn’t write this, I did – I’m one of his programmers.
@Jack:
The OOP model is actually pretty good. Yeah the syntax is a little weird but the prototype model is fantastic. You can monkeypatch the hell out of existing classes, even on already instantiated objects! How good is that! I don’t think even Python lets you do that.
Keith 2255 days ago
http://www.code-sucks.com/I second that. Love it
James 2255 days ago
http://www.jamesbooker.co.uk@Rob – Well that’ll teach me for not reading my GReader screen properly – it tells me at the top of the article that it was written by you.
Oh and yes, the ‘monkey-patching’ of pre-instantiated objects you describe is a very useful feature of Javascript, and allows you to get rid of some inheritance stuff if you like – sometimes you only ever want an extra function in a class for one specific purpose.
Christos 2254 days ago
http://namedb.net@Rob Haswell
If you want the utmost in flexibility, try Ruby, you can do anything you like at run-time with class, objects, etc… It’s by far the most flexible lang for the dynamic stuff (at least the most flexible that is widely used today).
@Dave
As far as the JS snippet above, that should/is possible with just about any programming language. I think what your missing is that your excited is because it can be done on the client, inside the browser, instead of performed server side.
Many issues arise with browsers when doing any heavy lifting with JS on the client (not that this alone is heavy lifting) – it’s a memory pig, and that “pigginess” is wholly controlled by the browser the user is on – meaning, in one browser some heavy JS lifting is fine, another, your spinning that users cpu pretty high.
JS on the client has been a good temporary solution. In reality, its replacement, or serious enhancement on each and every browser, is needed…
tom 2254 days ago
I’m glad you think JavaScript’s so great…and since there’s no way to hide it, everyone who also thinks your site’s JavaScript is great can easily rip it off…
The idea of saying that a client-side scripting language is better than the best server-side scripting language just seems really stupid to me.
How about, for example, the fact that users can deactivate support for your scripting language with one click in their browser’s preferences ?
The greatest language ever? Give me a break.
pilkster 2254 days ago
Totally off topic: Rob, you need to blog more often.
Rob Haswell 2254 days ago
Thanks. I’d like to, but Dave beats me violently every time he catches me not coding. I could only write this because he’s at Seodays right now. I’m sure I’m in for the thrashing of a lifetime when he gets back, but it’ll be worth it to get this off my chest!
Claudius 2254 days ago
JavaScript looks complex, any good site to for the basics? And what’s the best thing the JavaScript can do?
Rob Haswell 2254 days ago
http://www.bronco.co.uk/@Christos:
That feature (anonymous functions) can be done in pretty much any language, you’re right. However, support in my experience is sketchy. PHP’s create_function() is messy and I don’t think you can call a function immediately after defining it, eg create_function(..)(). Python’s lambdas wouldn’t cut it. I think C# has the necessary. Obviously I could also do it any language supporting eval(), that is if I wanted to cut up my good programmers’ club membership card.
It’s worth noting that JS in the browser is only slow for bad reasons. Compare ActionScript – same language, different stdlib+secmodel = wheeee.
@tom:
Well that’s a risk you take. If I was building something special like Gmail, yeah I’d obfuscate it. People are far too protective over code.
And consider: How do you know I don’t use JavaScript on the server? There are plenty of runtimes for ECMAScript you know
@James & Keith:
Sorry to burst your bubble, Dave hasn’t done programming for a long time
@Everyone:
If you want to read more posts on influential blogs about how great JavaScript is, try Joel On Software.
This comment written in Vim. Proudly.
Li Evans 2253 days ago
http://www.searchmarketinggurus.comI was about to post “Wow Dave you wrote a totally understandable post!”
This was a great post, programming geek girl I am, i like JS too. I actually understood the whole thing and didn’t shake my head once in thought of “WTF is he saying” and I didn’t have to restort to pulling out my DaveN translation guide I stole from the speakers conference room in London.
iain 2245 days ago
http://www.intraspin.com/search-engine-optimisation.htmlHere Here! I’m a JS fan :0)