调用函数两次?

Calling a function twice?

Jade.compileFile(layout, { pretty: false })(locals);

有人可以解释这段代码吗?

我知道此代码行使用了 Jade 模板引擎方法 compileFilelayout 是源路径,pretty 是传递给 compileFile 的选项。 locals 是一个对象。

这个函数是被调用了两次还是怎么回事?

在 .compileFile() 上形成 Jade docs:

Returns A function to generate the html from an object containing locals

在大多数JS模板引擎中,有一个解析原始模板字符串的编译函数,returns一个模板函数。

当使用数据(局部变量)调用函数时,它 returns 一个包含数据的 HTML 字符串。

来自 Jade 文档的代码示例:

var jade = require('jade');

// Compile a function
var fn = jade.compileFile('path to jade file', options);

// Render the function
var html = fn(locals);
// => '<string>of jade</string>'

我不知道 Jade 模板引擎,但第一次调用似乎是 return 一个函数。然后使用局部参数调用 returned 函数。抱歉,如果答案不正确。

Jade.compileFile 方法 returns 一个单独的函数,然后使用参数 locals 调用。

为了证明这一点,您也可以这样写:

var func = Jade.compileFile(layout, { pretty: false });
func(locals);