Javascript Jems - Lambda expressions
Written by Ian Elliot   
Tuesday, 14 January 2014
Article Index
Javascript Jems - Lambda expressions
Using anonymous functions and invocation

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.

 

jemsicon

 

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.

Summery

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.

Related Articles

Javascript Jems - First class functions

Chaining - Fluent Interfaces In JavaScript

JavaScript Objects With Value - valueOf and toString

JavaScript Hoisting Explained       

WAT! JavaScript, Ignorance And Prejudice       

What Exactly Is A First Class Function - And Why You Should Care     

Lambda Calculus For Programmers       

The Undefined Defined Variable       

 

To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, FacebookGoogle+ or Linkedin,  or sign up for our weekly newsletter.

 

blog comments powered by Disqus

 

Banner


Just JavaScript - Object Construction

Object creation is fundamental to all object-oriented languages, but in JavaScript is is left to the programmer to work out how best to do it and often the practice that you encounter isn't the best b [ ... ]



Just JavaScript - The Object Expression

As in most programming languages the expression is an important part of JavaScript, but it isn't quite the same. This is where the idea that JavaScript has some weird type conversions arises. But Java [ ... ]


Other Articles

<ASIN:0596805527>

<ASIN:193398869X>

<ASIN:0137054890>

<ASIN:1449381871>

<ASIN:1430230541>

 



Last Updated ( Tuesday, 14 January 2014 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.