Friday, October 1, 2010

JavaScript – It’s a Real Language!

JavaScript doesn’t get much respect. “It’s a toy!”, they say. The language has been around ever since the earliest browsers.  But did you know JavaScript is a real language? It seems that only front end developers realize that it is!  Where is the confusion?  You have to look at the language’s origins.  Traditionally, JavaScript has always been part of the browser.   This absolutely has contributed to the popularity it enjoys today, but has also mischaracterized it as a toy for browsers.  It has simply been used as the means to get the cool things done you can’t do with HTML alone.



Browsers Are So DOM

 

Many developers look at browsers as a single unit, but browsers have a dual personality. Part of the browser contains the Document Object Model, the DOM. The other part of the browser contains the JavaScript engine. You might be surprised at how this simple concept eludes many.  When a web page is written, the HTML is the source code for the DOM and the DOM is rendered to the screen.  How that DOM is rendered is where we end up getting into browser compatibility issues.  By using a developer utility like Firebug, select the HTML tab to see how the HTML is translated into a tree-like structure or a more exact representation can be found on the DOM tab.  Further, by changing those values manually we can see how the DOM effects what has been rendered on the screen.  Make no mistake here, JavaScript doesn’t write to the screen, JavaScript modifies the DOM, that effects the rendering of the screen.

Architecture Influences Opinion

 

Part of JavaScript’s perception problem is the prominence of poorly written code.  Many websites out there could be written better.  Coders for these sites often mix HTML elements with JavaScript, leaving the site with a big muddle of spaghetti code (it’s all mixed together).  It’s no wonder JavaScript has such a bad reputation.  To overcome this, code separation needs to be encouraged.  One needs to view the browser with distinct architectures, DOM and interpreter (HTML and JavaScript, respectively).   JavaScript is more effective when it has been abstracted into its own modules – separation from the HTML is important.  You can often see an HTML element defined with event attributes executing JavaScript.  These attributes like onkeypress, onclick, and onmouseover serve to confuse the design.  Although it is often easier and quicker to get it done that way, ultimately, it makes maintaining and debugging the code more difficult and that devalues the language.  Make sure you have an architect that will separate the code as early in the development cycle as possible – it simplifies design. Once this architectural abstraction is achieved, once you start thinking this way, you might be surprised at how easy it will be for you to visualize your coding tasks.

A Real Language is an Object Oriented Language

 

Object oriented languages have come to form a major foundation of the programming world.  Granted, there are other important approaches to follow like aspect oriented programming, modular design, etc, but OOP is very popular.  Object oriented languages come in many shapes and sizes.  SmallTalk, Java, C++ are all great, but they have varying degrees of how object-oriented they really are.  JavaScript is no different.  Closer to the C++ end of the chart, JavaScript was designed as more of a structured language, but it can be as OO as you want it to be.  JavaScript easily supports object inheritance and you can even create private members.

Although JavaScript makes extensive use of objects, the language itself has no object oriented requirements, meaning you can still write JavaScript code in a structured fashion.  But, once you choose to write code in an OO fashion, the addition of a foundation library can be helpful.  Dojo Toolkit is a foundation library that was written with an OO approach in mind and can assist you in achieving your object oriented structure.

JavaScript is Loose and Flexible

 

If you have coded for many years in a language like Java or C++, it is easy to make a premature judgment of JavaScript.  It can be easy to point to JavaScript’s lack of strictness as the reason it’s a toy.  After all, JavaScript does not have to define a variable in order to use it.  Nor do you have to define the type of structure a variable is to represent.  Is this looser style of definition wrong?  Well, the answer is in the eye of the beholder!  But, it makes for some short cuts in writing code that can help you get things out the door quicker.

It might surprise you that objects in JavaScript have an interesting advantage over those in compiled languages like Java.  In JavaScript, one can take any object and add new methods directly to it that were not there on creation.  This means if you receive the JSON object myobj from a server and you want to add methods to that object, it is simply a matter of saying:

var myobj = {};
myobj.myfun = function(txt) {alert(txt);};
myobj.myfun("hello");


From then on, your object has myfun available.  This can be a very powerful feature.  Suppose you receive an object from a server, wouldn’t it be nice to test the object and extend it with a method specific method instead of testing it repeatedly every time you wanted to perform a common action? You may be able to emulate this with other languages, but JavaScript has it built in.

more @ http://clubajax.org/javascript-its-a-real-language/

No comments:

Post a Comment