鍍金池/ 問答/HTML/ Node.js中的bind

Node.js中的bind

不理解console.error.bind是什么?
這里為什么要使用console.error.bind?

db.on('error', console.error.bind(console, 'error'));

直接使用console.error('error')輸出發(fā)生了錯誤,不好嗎?

回答
編輯回答
默念

是可以啊,可是db.on的第二個參數(shù)讓傳的是個函數(shù)啊,又不是函數(shù)的返回值。
不過我覺得倒是這么寫也可以

db.on('error', () => { console.error('error') };
2018年4月1日 23:16
編輯回答
還吻

達到的是同一個效果

console.error.bind(console, 'error')

可以作為一個函數(shù)使用

2018年2月25日 12:39