我知道關於call
和apply
在JavaScript中,但我目前堅持以下情況。包裝重新定義「this」的JavaScript回調
我使用Benchmarkjs,它有這樣定義的API:
.on(eventType, listener)
所以我會做,比如:
/* Register the onStart callback */
suite.on('start', onStart);
我onStart
函數將被調用這樣this === suite
。
我該怎麼做才能定義onStart
的arguments
之一?
我的代碼是這樣的:
foo = function() {
this.suite.on('start', this.onStart);
}
我想我onStart
功能有一個參考foo
。
有什麼建議嗎?
使用'bind'來覆蓋'this'並插入參數。 – 2013-02-15 14:01:53