Novemberborn, Straight lines circle sometime

JavaScript Terminology

This is an introductory tutorial to the sIFR Explained series, covering basic JavaScript (and programming) concepts — some knowledge of JavaScript, like knowing how to define functions, is required though. It’ll be updated from time to time when more background information is necessary. Here we go.

Anonymous Functions

An anonymous function is a function without a name. This is an anonymous function [1]:

function(msg){ alert(msg); }("hello world");

What is happening here is that the function is defined: function(msg){ alert(msg); } and executed immediately: ("hello world").

This isn’t an anonymous function:

function foo(msg){ alert(msg); };
foo("hello world");

Nor is this:

var foo = function(msg){ alert(msg); };
foo("hello world");

In general anonymous functions are used to execute code which only needs to be executed once. This is the way it is used in sIFR. A side effect which occurs in the sIFR code is the creation of closures.

Scope, the scope chain and closures

But before we get into closures, something about scope. Every JavaScript expression is surrounded in a scope. The scope contains what is available to the expression. When you define a new function, the body of this function exists in a new scope. For example:

function foo(){
    var msg = "hello world";
    alert(msg);
};

foo();

Here alert(msg); works, because msg is defined in the same scope as in which alert is called.

Deep down in the JavaScript interpreter the scope is represented as a call object. This object contains all the variables which have been defined in the scope that the call object represents. When a new scope is created, it’s call object is linked to the call object of the scope it was created in. This creates a scope chain.

This phenomenon is best explained through an example. In JavaScript the window object is the global scope. This is the highest scope in the scope chain. If we create a new function in the global scope the scope of this function will be chained to the window object:

var msg = "hello world";

function foo(){
    alert(msg);
};

foo();

Now, if you call foo, it’ll alert hello world! But how does it know the value of msg, which wasn’t defined in the function body foo? It turns out that if the JavaScript interpreter can’t find a variable in it’s current scope, it’ll go up the scope chain and searches the parent scope until it finds the variable. In our case it finds msg in global scope.

You can also create a scope chain by nesting functions:

function foo(){
    var msg = "hello world";
    function bar(){
        alert(msg);
    };
    bar();
};

foo();

Now, if you call foo, it’ll define bar and execute it immediately afterwards. And as expected it’ll alert hello world.

When a nested function has access to the scope of it’s parent function, it is called a closure. sIFR relies quite heavily on closures.

At ths point you might want to read Variable scoping gotchas from Scott Andrew for more information about defining variables in JavaScript.

Footnotes

[1]: I’m not really sure why you’d want to write an anonymous function to alert `”hello world”`, but it’s just an example!

link | javascript sifr | 20 April 2005, 17:00



Novemberborn: Extra

About the author

Mark Wubben is a hacker/writer in Enschede, the Netherlands.

Read more about Mark...

Go to

Jobs (NL)

Xopus zoekt programmeurs! Verbeter de code en win!

Please donate

If you like sIFR, please consider making a donation so I can spend more time on it. Thank you.

sIFR Documentation

See the documentation for sIFR 2 and sIFR 3.

Subscribe