JavaScript中是否有类C#的lambda语法?
是否有一个框架或后处理器的JavaScript支持lambda语法像在C#中?
CoffeeScript中的函数定义看起来像lambdaexpression式,但我没有深入研究它们。
任何人都可以告诉我,我可以在JavaScript中使用lambda语法?
ECMAscript 6中包含了类似语法的Lambda函数,它们被称为“箭头函数”。 一个例子:
["duck", "cat", "goat"].filter(el => el.length > 3);
返回["duck", "goat"]
目前支持Firefox和Chrome的最新版本。
为了在JavaScript中使用这种针对老版本浏览器的语法,有一些工具可以将ES 6编译成受到更广泛支持的版本,例如工具Babel或Traceur 。
你可以使用打字稿( http://www.typescriptlang.org/); :
function (d, i) { return "translate(" + dx + "," + dy + ")"; }
会成为
(d, i) => "translate(" + dx + "," + dy + ")"
还有更酷的东西,比如打字:…那就是如果你喜欢的话
我开始为jQuery创build一个扩展器来完成你所要求的事情。 JS-LAMBDA
例如在C#中:
coll.TakeWhile(x => x.index < 3);
好像:
$(coll).TakeWhile("x => x.index < 3");
精确的马修·麦克威!
我也想看看这个例子
"use strict"; /* Lambda Helper to print */ function PrintLineForElement(element, index, array){ document.write("a[" + index + "] = " + JSON.stringify(element)+"<br>"); } /* function to print array */ function Print(a){ a.forEach(PrintLineForElement) document.write("<br>"); } /* user model */ class User{ constructor(_id,_name,_age,_job){ this.id = _id; this.name = _name; this.age = _age; this.job = _job; } } /* Provaider users */ class UserService{ constructor(){ } GetUsers(){ //fake ajax response let data = []; data.push(new User(1,"Juan",20,"AM")); data.push(new User(2,"Antonio",20,"AM")); data.push(new User(3,"Miguel Angel",30,"Developer")); data.push(new User(4,"Bea",26,"AM")); data.push(new User(5,"David",24,"Developer")); data.push(new User(6,"Angel",24,"Developer")); data.push(new User(7,"David",34,"TeamLead")); data.push(new User(8,"Robert",35,"TeamLead")); data.push(new User(9,"Carlos",35,"TeamLead")); return data; } } const userService = new UserService(); const users = userService.GetUsers(); //get user order by name document.write("All users </br>"); Print(users); //get user order by name document.write("Order users by name ASC : users.sort((a,b)=>{return a.name > b.name })</br>"); Print(users.sort((a,b)=>{return a.name > b.name; })); document.write("Order users by name DESC : users.sort((a,b)=>{return a.name < b.name })</br>"); Print(users.sort((a,b)=>{return a.name < b.name; })); //get user filter by age document.write("Only users oldest or equal than 25 : users.filter( (w) => {return w.age >= 25;} )</br>"); Print(users.filter( (w) => {return w.age >= 25;} )); document.write("Only users smallest or equal than 25 : users.filter( (w) => {return w.age <= 25;} )</br>"); Print(users.filter( (w) => {return w.age <= 25;} )); //GroupBY /** * custom group by with Array.prototype.keys **/ Array.prototype.groupBy = function(f) { let groups = []; this.forEach( function( o ) { let group = f(o) ; groups[group] = groups[group] || []; groups[group].push( o ); }); return Object.keys(groups).map( function( group ) { return groups[group]; }) } document.write("Group user by age </br>"); Print(users.groupBy( (w) => {return w.age } )); document.write("Group user by job </br>"); Print(users.groupBy( (w) => {return w.job } )); document.write("Group user by job</br>"); Print(users.groupBy( (g) => {return [g.job] } )); document.write("Group user by job and age then order by name </br>"); Print(users.sort((a,b)=>{return a.job > b.job}).groupBy( (g) => {return [g.job+g.age] } )); document.write("Group user by job and age then order by name and filter where age < 25 and job equal AM </br>"); Print(users.sort((a,b)=>{return a.job > b.job}).filter((w)=>{ return w.age<25 || w.job == "AM" }).groupBy( (g) => {return [g.job+g.age] } ));
Javascript有非常好的匿名函数,可以让你在任何你需要的lambda函数中创buildlambda函数,但是这个实现带有一些重复的单词和范围创build:
function(name){ return "Hello, " + name; }
感谢EcmaScript 2015 lambda expresion现在可以用于更简单的语法,例如Arrow Function
(name) => "Hello, " + name
使用Typescript我创build了一个扩展Array<T>
类的List<T>
Array<T>
用法:
var list = new List<ListView>(); ... var selected = list.Items.Where(x => x.Selected);
执行:
export class List<T> extends Array<T> { public Where(callback: (value: T) => boolean, thisArg?: any): List<T> { try { const list = new List<T>(); if (this.length <= 0) return list; if (typeof callback !== "function") throw new TypeError(callback + ' is not a function'); for (let i = 0; i < this.length; i++) { const c = this[i]; if (c === null) continue; if (callback.call(thisArg, c)) list.Add(c); } return list; } catch (ex) { return new List<T>(); } } ... }
编辑:我创build了一个github回购: https : //github.com/sevensc/linqscript