LA.NET [EN]

Aug 20

Douglas Crockford says that the best part of Javascript is its implementation of functions. And I think that he’s absolutely right! The first thing you should keep in mind is that functions are objects in Javascript! The second interesting thing about functions is that they can be invoked (I guess that’s why they’re called functions, right?).

The simples way to create a function is through a function literal. Function literals start with the function keyword. Optionally, you can give it a name and define the expected parameters. All functions have a body delimited by curly braces. Here’s a really simple named function:

function sayHi(name) {
    alert(name);
}
sayHi("luis");


Even though you can keep introducing named functions, you’ll soon find out that this is not really a good idea. The main problem is that by adding definitions like the one in the previous snippet, we’re really adding methods to the global namespace and we’re increasing the chances of conflicts with other libraries (which leads to overwriting, not errors). The following snippet should show you why this is bad:



function sayHi(name) {
    alert(name);
}
function sayHi(name) {
    alert("hoho!");
}
sayHi("luis");



If you recall a previous post on scopes and contexts, then you’ll probably remember that functions are always associated with a context (which you can reference from within the function’s body by using the this identifier). Besides the this identifier, there’s also the arguments identifier which you can use to access the parameters that are passed to the function.



Notice that you wont’ get an error when the number of values passed to the function doesn’t match the declared parameters. The only insurance you have is that you can get all the values passed to the function through the arguments identifier. arguments also let us access the current function (arguments.callee) and the function that performed the current call (arguments.caller). The callee property is useful for recursion scenarios on anonymous functions. Look at the following example:



var factorial = function(number) {
    if (number === 0) return 1;
    return number * arguments.callee(number - 1);
};
alert(factorial(3));


Since we’ve opted for introducing an anonymous function, the only available option for invocating it is relying on the arguments.callee property. Since it points to the “current” function, we can use the () operator to invoke it and get the desired recursion behavior.



There’s really lots of stuff to write about functions, but we’ll leave it for future posts. Keep tuned for more on Javascript.

2 comments so far

  1. Orion''s Belt
    5:35 pm - 8-20-2009

    That”s why usually no one uses functions like that, it”s costume to attach all them to a master object that will act as a namespace:

    var Global {
    doSomething : function(arg) {}
    };

    Global.doSomething();

  2. luisabreu
    10:32 pm - 8-20-2009

    Yep. that”s the recommendation…and there”s also the (function(){…})() idiom for running stuff, but I”ll come back to that in future posts.

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>