Douglas Crockford delivered a
series of Video lectures on the most misunderstood language - JavaScript. Below
you will find my quick notes:
JavaScript has no connection with Java and it has been named so by Netscape to collaborate
with Sun to win over Microsoft. The language is loose typed meaning that there
is no strict data type enforcement so an array may contain a mix of integers,
strings, objects, functions, etc. Unlike Java where class keyword is used to
define OOPs concepts, JavaScript uses functions and a special kind of prototype
object. Primitive values in JavaScript are number (only one type), boolean, string,
null and undefined. Undefined is the default value for any object. Everything
else is an object. Like any other language JavaScript offers various operators
and the type conversion is quite powerful. There are bitwise operators and as
they are implemented in a very bad way it is recommended to avoid them.
JavaScript has "for…in" statement which is used to iterate all of the members of
an object. Unlike in Java, "switch" statements can have string values. JavaScript
error handling is done using “try..catch..throw” much in the same way as Java. However, as
there are no classes there cannot be multiple catch statements for a single try
block. Avoid using "with" statement to limit the scope of the code block. It is
just ambiguous and error-prone. Scope is only defined within functions. Variables
are defined using var statement. Though optional, it is important to place var
in front of all variable definitions to avoid messing with global scope. Constructors
(discussed later) by default return “this” object.
An object in JavaScript is a
collection of name/value pairs. Names are strings and values can be of any type,
including other objects. Every object is like a little database. Curly braces are used to define name/values
pairs with names and values separated by “:” and each pair separated by “,”. JavaScript offers Prototypal inheritance.
This type of inheritance is different from classical inheritance. There are
no classes, packages or modules in JavaScript, instead functions and a special
prototype object is used to achieve inheritance. Prototypal inheritance is
way more dynamic and expressive. All objects inherit prototype object and is used to
augment properties dynamically. For example:-
function Rectangle (w, h) {this.width: w, this.height:h}
If we define an instance of this Rectangle function like:
var r = new Rectangle(10,2)
r is a rectangle object with attributes width and height and
dynamically Rectangle could be augmented by
Rectangle.prototype.area = function () {return this.width*
this.height} //enables object r to have area attribute.
It is
important to differentiate between classical and prototypal inheritance. In
prototypal inheritance there are no rigid classes. Sub-class objects are
created on existing objects using customized Douglas
object function:
function object(o) {
function F() {}
F.prototype = o;
return new F();
}
var o = new Rectangle()
var sr = new object(o) // subclass of rectangle.
New objects can be decalred by curly braces or “new Object ()” or “object
(Object. prototype)” as
var o = {}; // best way
var o1 = new Object();
var o2 = object(Object.prototype);
Members in an object could be deleted using “delete”
operator.
Arrays are special types of objects. They are declared with
‘[]’. They are dynamic so there size can be increased after initialization (unlike in C). Index starts from 0 as in Java. As in other languages JavaScript provides various array
utility methods. Though arrays are objects they must be used when the names are
sequential integers. Like objects prototype could be used to augment array
properties.
Functions are objects. Functions can be defined both as
statements and literals:
var func = function func() {} // statement
var func = function() {} // literal
var func = function func() {} // literal (from 1.5)
Functions can contain inner functions. Inner functions have
access to variables and parameters that it is contained within. This brings to
an interesting concept “Closure” where inner functions have access to outer
function variables even after outer function returns. When a function is
contained in an object it is known as a method. While function invocation extra
parameters are ignored and missing parameters will have undefined value. There
are four ways to invoke a function:
functionObject(arguments) // function form
thisObject.methodName(arguments) // method form
new functionObject(arguments) // Constructor form
functionObject.apply(thisObject,[arguments]) // Apply form
Though constructor form is a function invocation, it has to
be preceded with “new” keyword. In order to treat constructors different it is
recommended to have the first function name start with capital letter.
Parameter “arguments” an array-like object is available in the function. Statement
“eval” and “Function” should be used cautiously.
Avoid using global variables. Wrap the application in an
anonymous function like:
YAHOO.Trivia =
function () {
// define your common vars here
// define your common functions here
return {
getNextPoser: function (cat, diff) {
...
},
showPoser: function () {
...
}
};
} ();
(direct copy from Douglas presentation)
JavaScript is a dynamic language so there is no need for
classical inheritance pattern. It is neutral on threads concept. On the client
side (browser) JavaScript runs in a single thread. Coding style standards
should be practiced more importantly in JavaScript as it is untyped/soft language. More information about coding conventions is available at http://javascript.crockford.com/code.html.
Presentation is available at http://developer.yahoo.com/yui/theater/ titled “Douglas Crockford — "The
JavaScript Programming Language". Thanks to douglas and yahoo.
My opinions
Douglas points out many design flaws and one of the primary reasons I feel is to emulate Java features in JavaScript. Trying to borrow classical language features in a prototypal
language won't work well and this including many other reasons led to confusion in the community.
Advent of Ajax made JavaScript more powerful and may
fulfill yester year’s netscape dream of delivering robust/powerful applications
via web browser eliminating the dependency on OS.