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

后端 未结 6 667
生来不讨喜
生来不讨喜 2020-12-02 12:13

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 ha

6条回答
  •  孤城傲影
    2020-12-02 12:34

    FIDDLE: https://jsfiddle.net/vktawbzg/

    NPM: https://www.npmjs.com/package/linqscript

    GITHUB: https://github.com/sevensc/linqscript

    using Typescript i created a class List which extends Array

    usage:

    var list = new List();
    ...
    var selected = list.Items.Where(x => x.Selected);
    

    implementation:

     export class List extends Array {
    
        public Where(callback: (value: T) => boolean, thisArg?: any): List {
                try {
                    const list = new List();
    
                    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();
                }
            }
    ...
    }
    

    EDIT: I created a github repo: https://github.com/sevensc/linqscript

提交回复
热议问题