javascript - JS Currying 一个静态函数

标签 javascript class google-cloud-platform google-cloud-functions currying

我有一段代码如下。

// controller.js
class Controller {
    static easyPost = _post(EasyAuth, EasyValidate, EasyHandle);

    static hardPost = _post(HardAuth, HardValidate, HardHandle);

    static _post(auth, validate, handle) {
        return (req) => {
            if (auth(req) && validate(req))
                handle(req);
        }
    }
}

module.exports = Controller;

//controllermapper.js
const Controller = require("./controller");

class ControllerMapper {
    static map(req) {
        switch (req.path) {
            case "/easyPost":
                Controller.easyPost(req);
                break;
            case "/hardPost":
                Controller.hardPost(req);
                break;
        }
    }
}

背后的想法是我可以通过currying创建easyPosthardPost。因为他们的总体流程非常相似。

但是,当我尝试将代码推送到 Google Cloud Function 时。它抛出一个错误,如

> Detailed stack trace: /user_code/controller.js:6
>     static easyPost = _post(EasyAuth, EasyValidate, EasyHandle);
>                     ^
> 
> SyntaxError: Unexpected token =

我应该怎么做才能解决这个问题?


编辑

我把我的代码改成了

// controller.js
class Controller {
    static easyPost(req) {
        Controller._post(EasyAuth, EasyValidate, EasyHandle)(req);
    }

    static hardPost(req) {
        Controller._post(HardAuth, HardValidate, HardHandle)(req);
    }

    static _post(auth, validate, handle) {
        return (req) => {
            if (auth(req) && validate(req))
                handle(req);
        }
    }
}

如果这是一个好的做法,可以对此发表评论吗?

最佳答案

这会在此刻起作用

class Controller {
    static _post(auth, validate, handle) {
        return req => {
            if (auth(req) && validate(req)) handle(req);
        };
    }
}
Controller.easyPost = _post(EasyAuth, EasyValidate, EasyHandle);
Controller.hardPost = _post(HardAuth, HardValidate, HardHandle);

有一个第 3 阶段提案,您编写的代码可以正常工作 - https://github.com/tc39/proposal-static-class-features/

不太像写的那样 - 您需要按如下方式指定 Controller._post

class Controller {
    static _post(auth, validate, handle) {
        return (req) => {
            if (auth(req) && validate(req))
                handle(req);
        }
    }
    static easyPost = Controller._post(EasyAuth, EasyValidate, EasyHandle);
    static hardPost = Controller._post(HardAuth, HardValidate, HardHandle);
}

注意:您可以在 babeljs 中启用它,但它在 babel 中处于第 2 阶段

关于您更新的代码 - 您仍然需要调用 Controller._post 而不仅仅是 _post

class Controller {
    static easyPost(req) {
        return Controller._post(EasyAuth, EasyValidate, EasyHandle)(req);
    }

    static hardPost(req) {
        return Controller._post(HardAuth, HardValidate, HardHandle)(req);
    }

    static _post(auth, validate, handle) {
        return (req) => {
            if (auth(req) && validate(req))
                handle(req);
        }
    }
}

为了证明您必须使用 Controller._post,这里是您代码的精简版本

class Controller {
    static succeeds(req) {
        return Controller._post()(req);
    }

    static fails(req) {
        return _post()(req);
    }

    static _post() {
        return (req) => {
          	return `got ${req}`;
        }
    }
}
console.log(Controller.succeeds('ok'))
console.log(Controller.fails('ok'))


最后,您不必使用 Controller._post

的代码版本
const post = (auth, validate, handle) => (req) => {
    if (auth(req) && validate(req)) {
        handle(req);
    }
};
class Controller {
    static easyPost(req) {
        return post(EasyAuth, EasyValidate, EasyHandle)(req);
    }

    static hardPost(req) {
        return post(HardAuth, HardValidate, HardHandle)(req);
    }

}
module.exports = Controller;

现在 post 也是私有(private)的

虽然,如果我写这个,我会简单地做

const post = (auth, validate, handle) => (req) => {
    if (auth(req) && validate(req)) {
        handle(req);
    }
};
class Controller {
}
Controller.easyPost = post(EasyAuth, EasyValidate, EasyHandle);
Controller.hardPost = post(HardAuth, HardValidate, HardHandle);
module.exports = Controller;

或者,如果这就是 Controller 的全部内容

const post = (auth, validate, handle) => (req) => {
    if (auth(req) && validate(req)) {
        handle(req);
    }
};
module.exports = {
    easyPost: post(EasyAuth, EasyValidate, EasyHandle),
    hardPost: post(HardAuth, HardValidate, HardHandle)
};

关于javascript - JS Currying 一个静态函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52585009/

相关文章:

ruby - 可以告诉 ruby​​ 我是否在给定模块中定义了给定类

java - Quarkus:如何将复制的 SQL 数据库与读/写实例连接起来?

google-cloud-platform - 如何使用带有 GPU 的计算引擎创建数据实验室?

javascript - 单击按钮时更改 div 的 CSS

javascript - Chrome 61 : Unexpected token import

php - 使用 jquery 发布和返回数据会损坏 å ä ö

php - 如何使用 javascript 动态更改 HTML 值

java - 尝试随机掷两个骰子并将总和相加直至达到二十一

swift - 为什么常量约束结构实例的属性而不是类实例的属性?

firebase - 如何为 firebase 创建服务帐户以与 firebase 功能一起使用