JavaScript is powerful, people. Believe me.
There are many people who still believe that JavaScript is somehow an inferior programming language, and that Java or C++ is somehow better. Today I shall attempt to put some of these beliefs to rest.
First a little history. JavaScript started in the Netscape browser as a new feature to combat the competing Internet Explorer (we can see how well that worked out). It was originally called Mocha, but Netscape decided to change the name to JavaScript after a deal with Sun to include Java technology in the browser. Of course, Microsoft had to have their own JavaScript, and called their JScript to avoid potential trademark concerns. As of this blog, the latest JavaScript is 1.7.
JavaScript and Java are, for the most part, only related by name. Their similar syntax is because both are derived from the C programming language (about which I have previously commented). While Java is a statically typed language (each variable has a specific type), JavaScript is dynamically typed (variables' types are determined by context). It is this superficial similarity that leads to our first misconception.
1. Doesn't JavaScript lack support for Object Orientation?
Many programmers who are not practiced in JavaScript will say that JavaScript does not support object oriented programming because it does not support classes. This belief is, of course, wrong. It is true that JavaScript does not use the traditional Class-based paradigm used by C++ and Java, in which objects are instances of classes, and new classes can be built by inheriting from other classes. Instead, it follows the Prototype-based paradigm, in which new objects can be built by cloning an existing object or ex nihilo (“from scratch”).
In JavaScript, a new object can be created in multiple ways. The first is through an object literal, such as:
var myObj = { property: "someValue" };The second is through a constructor function, like the following:
function MyType(a)
{
this.property = a;
}
var myObj = new MyType("someValue");Both create a new object with one property, whose value is the string
"someValue". The second is more like the C++ and Java style of object creation and simulate a class.JavaScript (and all Prototype-based language) also provide a mechanism for inheritance. This is where the prototype comes in to play. Just what is the prototype? It is an object upon which other objects are built. For example:
function BaseType()
{
this.funcA = function()
{
alert("Base::funcA");
};
this.funcB = function()
{
alert("Base::funcB");
};
}
function DerivedType()
{
this.funcB = function()
{
alert("Derived::funcB");
};
}
DerivedType.prototype = new BaseType();
var myDeriv = new DerivedType();
myDeriv.funcA();
myDeriv.funcB();The above will display the following:
Base::funcA
Derived::funcBEvery DerivedType starts out with all the functions of a BaseType, but the constructor then overrides the property funcB with its own version. This is inheritance.
2. Isn't JavaScript only used in web pages?
This one is pretty easy to refute. JavaScript is used in the core of the Mozilla Firefox browser, and when writing extensions one uses JavaScript. There is even a command line version of the JavaScript engine, so that JavaScript shell scripts can be made.
3. Doesn't JavaScript not always work? Isn't it unstandardized?
The official standard for JavaScript is called ECMAScript. If there is a standard, why does JavaScript work in some browsers and not in others? The question answers itself: not all browsers implement the same JavaScript. They all implement ECMAScript, but this is the barest form of JavaScript. The parts that usually vary are the DOM and the event model.
The DOM (Document Object Model) is a way of expressing XML documents (usually the HTML page) as a collection of objects in parent-child-sibling relationships. The W3C has recommended a standard DOM, defining objects and their functions. Most browsers follow this recommendation in their implementation of ECMAScript, but some (read: MS Internet Explorer) do not. This makes writing cross-browser scripts that use the DOM difficult, and has led many to think that JavaScript is unreliable.
var xyz = document.getElementByID('xyz');The same code in JScript:
var xyz = document.all['xyz'];Another issue is the event model. Again, there is a recommended standard from the W3C for how events should work. And again, some browsers (guess who!) have decided not to use the standard.
JavaScript:
function listener(e)
{
alert(e.srcElement);
}
xyz.addEventListener('onclick', listener, false);The above in JScript:
function listener()
{
alert(window.event.srcElement);
}
xyz.attachEvent('onclick', listener);There are a few significant differences. First, notice that in the W3C standard, the
listener function takes the event object as a parameter, but the JScript version has a global event object. Secondly, notice that the implementations use different functions with different numbers of parameters to add an event to an object.Act III: Epilogue
I hope we've learned something from this rant. JavaScript is not to be taken lightly; it can be used to do anything another programming language can do, because it is what is a Turing-complete language. JavaScript is sometimes difficult to use in web pages because different browsers use different implementations of the ECMAScript standard, but most browsers use compatible language features, but Internet Explorer does not.
Labels: computer science, computers, JavaScript



