Javascript - Introduction to the language

New variables in JavaScript are declared using the var keyword:

var xyz;
var xyz = "hello";

If you declare a variable without assigning it any value, its value is undefined.

Adding an empty string to a variable is useful way of converting it.

The double-equal operator perform type coercion if you give it different types. To avoid type coercion, use the triple-equal operator.

There are basically two ways to create an object:

var obj = new Object();

and

var obj = {};

These are sematically equivalent; the second is called object literal syntax, and is more convenient. Object literal syntax was not present in very early versions of the language.

Once created, an object's properties can be accessed in one of two ways:

obj.name = "Simon";
var name = obj.name;

and

obj["name"] = "Simon";
var name = obj["name"];

These are sematically equivalent. The second method has the advantage that the name of the property is provided as a string, which means it can be calculated at run-time. It can also be used to set and get properties with names that are reserved words:

obj.for = "Simon"; // Syntax error
obj["for"] = "Simon"; // works fine

The old way of creating arrays:

var a = new Array();
a[0] = "dog";
a[1] = "cat";
a[2] = "hen";

A more convenient notation is to use array literal:

var a = ["dog", "cat", "hen"]

A javascript function can take 0 or more named parameters. The function body can declare its own variables which are local to that function. The named parameters turn out to be more like a guideline. You can call a function without passing the parameters it expects, in which case they will be set to undefined. You can also pass in more arguments than the function is expecting. Functions also have access to an additional variable inside their body called arguments, which is an array-like object holding all the values passed to the functions.

Javascript lets you call a function and call it with an arbitrary array of arguments, using the apply() method of any function object:

function avg() {
    var sum = 0;
    for (var i = 0, j = arguments.length; i < j; i++) {
        sum += arguments[i];
    }
    return sum / arguments.length;
}
avg.apply(null, [2, 3, 4, 5]);

The second arguments to apply() is the array to use as arguments; the first will be discussed later on. This emphasizes the fact that functions are object too.

Javascript lets you create anonymous function.

var avg = function() {
  // function body
}

This is extremely powerful as it lets you put a full function definition anywhere that you normally put an expression.

Javascript allows you to call function recursively. This is particularly useful for dealing with tree structures.

How can an anonymous function call itself recursively if they don't have a name? The answer lies with the arguments object, which in addition to acting as a list of arguments also provides a property called arguments.callee. This always refers to the current function, and hence can be used to make recursive calls:

var charsInBody = (function(elm) {
    if (elm.nodeType == 3) {
        // TEXT_NODE
        return elm.nodeValue.length;
    }
    var count = 0;
    for (var i = 0, child; child = elm.childNodes[i]; i++) {
        count += arguments.callee(child);
    }
    return count;
})(document.body);

Since arguments.callee is the current function, and all the functions are objects, you can use arguments.callee to save information across multiple calls to the same functions. Here is a function that remembers how many times it has been called:

function counter() {
    if (!arguments.callee.count) {
         arguments.callee.count = 0;
    }
    return arguments.callee.counter++;
}
page_revision: 0, last_edited: 1227374297|%e %b %Y, %H:%M %Z (%O ago)
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License