javascript - 为什么 NodeJS 的绑定(bind)函数在这种情况下停止工作

标签 javascript node.js function bind

为什么绑定(bind)函数对于以下代码停止工作?

function exitHandler(options, err) {
  console.log('exit');
  if (options.cleanup) console.log('clean');
  if (err) console.log(err.stack);
  if (options.exit) process.exit();
}

//do something when app is closing
//process.on('exit', exitHandler.bind(null,{cleanup:true})); process.exit()

// or
process.on('exit', function() {
  exitHandler.bind(null,{cleanup:true})
});

如果我取消注释 process.on('exit', exitHandler.bind... 行,它会正常工作。

最佳答案

我认为这是因为绑定(bind)创建了一个新函数,所以在第二个示例中它实际上并没有触发该函数。在第一种情况下,它确实会被解雇。

关于javascript - 为什么 NodeJS 的绑定(bind)函数在这种情况下停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48968240/

相关文章:

javascript - Meteor 中登录尝试的 DDP 速率限制器

javascript - 在 Canvas 中使用 globalCompositeOperation 遮蔽多个形状

javascript - Node、express 和 Angular 的重定向问题

node.js - 具有 Express 和 nginx 反向代理的 Vue Router 历史记录模式

r - "If"、 "else"、 "is.na()"在 R 的一个函数中的用法

javascript - 无法点击我网站上的任何链接

javascript - 如何为 Async.waterfall 数组 Node.js 中的函数编写单元测试

javascript - 如何在javascript中使用另一个对象生成多个对象

node.js - 调用在 Controller 中同步工作的函数 - Node

javascript - 有没有办法将多个参数传递给 jquery 函数