Lambda calculus (also written as λ-calculus) is a formal system in mathematical logic for expressing computation based on function abstraction and application using variable binding and substitution
what was that? ok, let me put it this way: lambda means a simple mapping relation from
x to y, but in most programming languages, it also refers to anonymous function, as in most
case it should only be used once.
lambda can be pass as parameter to other function, or return by other function. this kind of
things also is called higher order function1.
but the original version of lambda expression is much simpler then anonymous function. It should only
take one argument.
let’s take a look at the new version of JavaScript lambda.
arrow function
ECMAScript 6, code name harmony
(as you can see through this link, Firefox implemented most of the features)
is RC now. Without waiting for all browsers support, through some tramscompiler such as Babel,
we can use most of es6 features in our product code already2。
One of the most exciting feature is arrow function. Why all it arrow fucntion?
You may still remember lambda? A arrow make the anonymous function more like lambda.
arrow function is implemented from Firefox 223, alternately you can
use in chrome by toggle on the es6 features4.
So, it would be better if you run all examples code in firefox.
define a arrow function
There are two form of defining a arrow function
Why Arrow
Here is the old way of doing map multiple 2 to an array.
and with arrow function will be:
So, we just remove function and return and {}? Indeed, yes, those are all
symbols we never needed.
map(x => x*2) make more sense, much more sorter, readable and descriptive.
Lexical this
If you’re still not convinced to use arrow function, here comes
the real reason you should start using it.
phwww, no more manual fixing this, arrow function always point to the instance object.
<a class=”jsbin-embed” href=”http://jsbin.com/vawobe/1/embed?js,console”>codes in jsbin</a><script src=”http://static.jsbin.com/js/embed.js”></script>
Anonymous function in JavaScript
In JavaScript, function is first class citizen, which means function
can be return or pass as arguments.
define anonymous function
Creating a anonymous is never been so <del>easy</del>:
1 function(x){2 returnx*x;3 }// => SyntaxError: function statement requires a name
Opss, what’s going on here? Why function without a name is a SyntaxError?
We define a function here using function expression, which means a value
should be returned:
1 vara=newArray()// this is a statement, while "new Array()" is expression
That’s why the error message says function statement requires a name. function statement
is the way compiler think you’re trying to use.
While the funny thing is, passing such a function as argument or to a variable is fine:
1 varsquareA=function(x){2 returnx*x;3 }
But this is weird since now squareA become a named function.
But the way how they are created is different. squareB is created via
function statement, while squareA is created vie function expression as a anonymous function
, then assign to a variable: