Subscribe via RSS

On JavaScript

Going Back After Two Years


January 25th, 2008 last updated March 14th, 2008

Two years ago, I would have called myself a fairly good HTML/CSS developer - nothing amazing, but I certainly had a strong grasp of how to do it, and do it right. This all stemmed from reading several books on the topic, most notably HTML & XHTML and Cascading Style Sheets. Of course, the obvious next step was to learn JavaScript, so I picked up Javascript for Web Developers", under the (foolish) impression that I could just jump right in and learn how to program. Admittedly, reading through it I comprehended enough to build my first script but it really didn't catch on until I read further books, including my favorite, PHP and MySQL Web Development. This is quickly turning into a plug for programming books, huh? Nevertheless, I was inspired to re-read the JavaScript book because, as I said, I really didn't learn much, and have never really done much of anything with JavaScript.

Now that I have a much stronger background in programming - two years of hands on experience with PHP, Java, Scheme, and programming concepts such as Object Orientation - I am learning so much more reading this book again; and I'm amazed that I learned anything, this book is so incredibly dense! Part of the reason why I stopped working with JavaScript once I learned PHP was because I felt it was quite convoluted to work with. Nevertheless, JavaScript is a very interesting language, a strange combination of great power and extreme confusion. So far I've read the chapters on ECMAScript, the underlying language of JavaScript (similar to XML underlying XHTML) which defines the grammar and language structure. For those who don't know, the remainder of JavaScript is the definitions that connect ECMAScript with the document and the browser.

There are some basic grammar issues which I find questionable, for instance how functions handle parameters; you can only define a function once (no overloading) but it really doesn't matter what number of parameters you set, because you can call the function with however many parameters you want, it'll still work. Kind of like PHP's ability to set default values for a function, but everything is optional, and you can actually pass more parameters than were set, and there aren't any default values. This is admittedly a powerful tool, but it seems to me like it's very easy to create completely incomprehensible errors.

The other interesting thing about functions is that they are in fact reference variables which can be passed, assigned, modified, and the like, very similar to Scheme and other functional programming languages. The only thing I don't think these functions can do (though it would be possible to implement this in the function) is take a subset of all the necessary parameters and return a function which only needs the remainder of the parameters. What I mean by that (in Scheme):

(+ 5 3) ; the plus function, returns 8 (+ 5) ; returns a function which takes one parameter and adds five to it

The ability to pass functions as parameters to other functions gave me the idea that JavaScript could be a nice introduction to functional programming for people who are used to more 'normal' languages such as C, Java, and PHP. I'm not sure how well this would work in practice, due to the extra step of having to learn JavaScript, but it might be interesting.

Another very strange feature is how ECMAScript deals with objects. Everything is an object, similar to Java, and therefore any variable, primitive or object can be assigned values and have functions called. ECMAScript is also a fully operational OO language, meaning it allows for Inheritance, Encapsulation, and Abstraction. However there is no real way to create and define your own objects. Other OO languages have a class definition which sets the template for a class of object. Having class definitions written into the languages will ensure a structured and uniform program. But structure and uniformity seems to be JavaScript's primary failing, so of course they don't have a real way to define classes. There are several (more than half a dozen) different ways to accomplish the goal of defining a class, but all of them seem to have some sort of limitation on how you can use the objects created - certain methods break inheritance, others create duplicate functions for each instance, it doesn't act like an object visually, or the like.

There are no doubt more problems (and features) of JavaScript, which I'll continue to address as I finish reading the book, but for now, my biggest impression is one of wonder - how did I possibly read this book two years ago? Surely absolutely nothing made sense.

New Comment

Name:*
Email:*
Website:
Title:*
Comment:*
 
Your email address will never be displayed or shared. Your comment will appear once approved.