我不熟悉这种语法。我有一个函数设置来处理套接字请求,如下所示:
//Predefined function is stored in the sensors object
var sensors = { pre-defined-function: function(req, res, next){
console.log('Test');
next();
}
exports.make_socket_controller = function (sockets) {
return function (req, res, next) {
//Predefined function is called
sensors['pre-defined-function']
(req, res, function() {
//another function
}
如果我用实际函数本身替换sensors[pre-defined-function]
,我会收到语法错误
function(req, res, next){
console.log('Test');
next();
}
(req, res, function() {
//another function
}
另外,当一个函数的结尾以某种方式滚动到另一个函数时,它意味着什么?我从未见过一个函数的结尾后面紧跟括号。
答案 0 :(得分:1)
如果要立即调用它,需要在函数文本周围添加括号:
(function(req, res, next){
console.log('Test');
next();
})(req, res, function() {
// another function
});