The magic of JavaScript

7. Apr 2007

For us "strongly typed" guys JavaScript is a pretty ackward language. We're more or less used to that the compiler stops 90% of all of our bugs from reaching the runtime environment. In JavaScript everything is quite different. Note I said different and not worse! In fact I used to think of JavaScript as a weaker language for a very long time since it lacked all the goodies we're used to from more strongly typed languages like class declarations, private members/methods, encapsulation, coherence, etc... The list goes on into infinity! But when you've done JavaScript as much as I have done you also come to realize that this is also JavaScripts strength! Lets dive into one specific "weakness" of JavaScript; namely the class declaration (or lack of) problem...
Now lets say you've got a collection of something and you want to iterate that collection and "do stuff" on it. But you don't want to restrict the "do stuff" to some specific operation you want to make this logic generic so that users of your collection class can choose for themself what this "do stuff" should be. In C you're forced to using templates, overload the () operator or use abstract base classes for solving such a problem. In C# you're forced to using either interfaces or delegates while in Java you can only choose interfaces. In JavaScript you can just "do it" and that's where the magic kicks in!



This isn't really possible with neither C or C# without some major hassle, in addition the code would obviously be a lot larger and that's one of the key points! JavaScript is perfect for web projects since the amount of code the client has to download will be very small!
In JavaScript the above code doesn't need to implement some interface, you don't need to declare a template class/method, you don't need to overload the () operator etc, etc, etc...
Die hard C fanatics will obviously turn around while they're sleeping trying to explain why this is a bad solution, and for a desktop application it probably is, also for server code it's not the optimal solution but for downloading onto a client in a (bandwidth) constrained environment it's just what the doctor ordered!
JavaScript rules some places just like any other language rules some places...

AddThis Social Bookmark Button Add to DZone AddThis Feed Button


Tags: programming javascript
Comments
RE: The magic of JavaScript
Posted 1. Jun 2007 by anonymous

 
Legal note: the meaning and content expressed on these blogs don't necassary correspond with the meaning of the legal company Gaiaware AS, but are considered to be personal opinions and expressions.