lambdas

/* main syntaxes of lamda functions
   (param1, param2, …, paramN) => { statements } 
   (param1, param2, …, paramN) => expression
*/

var lambda1 = (a) => {return a + 100;}
// or same
var lambda2 = (a) => a + 100;


["1", "12", "123"].map( el => el.length)
       .forEach( (el, ind) => {console.log(`maped[${ind}]=${el}`); } );

var lambda = arg => ({b: arg});
console.log(lambda(34).b);   

var person = {
    firstName : "Harry",
    lastName: "Potter",
    fullNameWrong : () => {return `${this.firstName} ${this.lastName}`;},
    fullName: function (){return `${this.firstName} ${this.lastName}`;}
    };

console.log(person.fullNameWrong()); // undefined undefined
console.log(person.fullName());   // Harry Potter    

Lambdas look like shorthand for anonymous functions, but:

  • do not have their own arguments object
  • do not have own this, so be carefully use them as methods
  • do not have prototype property
  • cannot be used as constructors and will throw an error when used with new operator
  • can have concise body - just only expression without curly braces and return statement

In JavaScript lambdas also known as arrow functions.

Parameters of lambda and arrow must be on same line.

When lambda has single parameter, round braces can be omitted.

When a lambda returns the result of a single expression, curly braces and the return statement can be omitted, i.e. you can use lambda with expression syntax.

When lambda returns a object literal, last must be in round braces.

Internet Explorer 11 doesn't support lambdas.