A more familiar alternative

Things are even simpler because you don't have to store a reference to a function to pass it to another function or to make use of it.

In other languages this facility is often called an anonymous function. In JavaScript is is just a use of the idea of an object literal i.e an object crated on the fly as in {myProperty:"value"}.

Anywhere you can use an object reference you can use an object literal.

JavaScript also supports anonymous functions as object litterals and you can write this sort example as:

list.sort(function(a,b){ return a.length-b.length;});

This last example looks a lot more like the sort of typical use that lambda expression are put to in other languages.

In say C# you could write the same thing using

list.sort((a,b) => a.length-b.length)

and the expression (a,b) => a.length-b.length is a lambda expression. Notice that apart from being a little more compact - no need for the keywords function and return - it is more or less the same.

Thus JavaScript with its first class functions or functions as objects doesn't really need lambda expressions or it already has them depending on your point of view.

Of course there is nothing stopping you from creating your own functions that accept other functions. As long as you have fully accepted the "functions are just objects" idea you probably don't need to see an example of how to do this but for completeness:

Say=function(t){ t();}

This will simply call any function that you pass to it. If you define:

Hello=function(){ alert("Hello");}

Then

Say(Hello);

will call the Hello function and display an alert.

Notice that you have to pass the variable that references the function without accidentally invoking the function.

That is don’t write:

Say(Hello());

by mistake as this would call the Hello function and then pass its result to the Say function which as a string couldn't be called as a function.

You need to distinguish very clearly between passing a function and passing the result of a function to another function.

Function Invocation ()

You also need to get into the habit of thinking of the () brackets as being the function invocation operator. When ever you use () following an expression it calls the function that is the result of the expression.

For example you can write:

(Say)(Hello);

or even:

Say=function(t){ t();}(Hello);

The round brackets at the end of the function definition call the function as soon as it has been defined.

This is usually called an "Immediately Invoked Function Expression" or IIFE and it is very useful and used a lot in idiomatic JavaScript.

This instant function evaluation can be useful when you want to pass the result of a "lambda expression".

For example:

alert(function sum(a,b){return a+b;}(1,2));

Here we have a lambda expression, the sum function being defined and evaluated in a single step.

The result, 3 in this case is passed to the alert function.

Although this example isn’t particularly useful it does become useful when combined with other Javascript features such as closure – more of which another time.

Summary

So what to take away -

In JavaScript functions are function objects and they can be created and used in the same way.

The only difference between an object and a function object is that a function object has a default method i.e. the code that is usually considered to be the function.

Once you understand JavaScript's first class functions you realize that you don't need to introduce anything extra to the language such as lambda expressions, delegates or anonymous functions to do the same tasks.

Superficially a function object literal looks a lot like a lambda expression and it can mostly be used in exactly the same way.

This means you can pass functions to other functions store them in arrays and generally make use of them as standard objects.

There are some hidden aspects of using jQuery and its data manipulation functions represent hidden gold. If you have struggled to work with data in JavaScript, and with the DOM in particular, jQuery h [ ... ]

JavaScript is a very subtle and sophisticated language and it deserves to be treated in its own right and not as a poor copy of other object-oriented language. In this first chapter of Just JavaScript [ ... ]