Functions in Javascript – part I

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) {

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) {
function sayHi(name) {

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);

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.


~ by Luis Abreu on August 20, 2009.

2 Responses to “Functions in Javascript – part I”

  1. 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) {}


  2. 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

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: