【问题标题】:coffeescript passing require() parameters wrongcoffeescript 传递 require() 参数错误
【发布时间】:2016-02-01 15:58:54
【问题描述】:

代码require ('./routes') app 编译为require('./routes'(app)); 但我需要它编译为require('./routes)(app)。我怎么能这样做?

【问题讨论】:

    标签: node.js express parameters coffeescript require


    【解决方案1】:

    括号在 CoffeeScript 中有两个用途:

    1. 他们将表达式分组:(a + b) * c
    2. 它们用于调用函数:f(x)

    当你这样说时:

    f (x)
    

    x 周围的括号的含义有些模糊;他们是分组括号还是函数调用括号?如您所见,CoffeeScript 选择了前者。

    如果你想(或需要)使用括号来调用函数,你不想在左括号之前有一个空格,你想要:

    f(x)
    

    在你的情况下,你想要:

    require('./routes') app
    

    甚至:

    require('./routes')(app)
    (require './routes') app
    

    【讨论】:

    • 这是另一种写法:(require './routes') app
    • @LeonidBeschastny:是的,那个也是。我倾向于忘记那个版本,因为它看起来太像 Lisp。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-18
    • 2015-10-08
    • 1970-01-01
    • 2012-07-20
    • 1970-01-01
    相关资源
    最近更新 更多