Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a C#-like lambda syntax in JavaScript?

Tags:

javascript

Is there a framework or post processor for JavaScript that supports lambda syntax like in C#?

Function definitions in CoffeeScript seem to look like lambdas but I have not looked into them thoroughly.

Can anyone tell me, can I use lambda syntax in JavaScript?

like image 859
Élodie Petit Avatar asked Aug 25 '11 12:08

Élodie Petit


People also ask

Why is there a slash in AC?

Senior Member. You read it as "ei-cee" (no "slash" pronounced). In terms of distinguishing between "air conditioning" and "air conditioner," I can think of an example like "Today, I bought a new air conditioner" ("conditioning" not allowed). I personally would not say "Today, I bought a new AC."

Why is it called AC?

Air conditioning, often abbreviated as A/C or AC, is the process of removing heat from an enclosed space to achieve a more comfortable interior environment (sometimes referred to as 'comfort cooling') and in some cases also strictly controlling the humidity of internal air.

What do you mean by AC?

a/ c is an abbreviation for air-conditioning. Keep your windows up and the a/c on high.


2 Answers

Lambda functions with similar syntax are included in ECMAscript 6, they're known as "arrow functions". An example:

["duck", "cat", "goat"].filter(el => el.length > 3); returns ["duck", "goat"]

There's currently support in recent versions of Firefox and Chrome.

To use this syntax in JavaScript that's targeting older browsers there are tools that can compile ES 6 to a more widely supported version - for example the tools Babel or Traceur.

like image 112
Matthew Mcveigh Avatar answered Sep 20 '22 00:09

Matthew Mcveigh


You could use typescript (www.typescriptlang.org/):

function (d, i) {     return "translate(" + d.x + "," + d.y + ")"; } 

would become

(d, i) =>  "translate(" + d.x + "," + d.y + ")"   

and much more cool stuff, like typing: ... that's if you are into that

like image 43
hans Avatar answered Sep 17 '22 00:09

hans