node.jsにモジュール/クラスを作成して非同期実行時間を測定しようとしていますが、何が問題なのかわかりません。次のクラス「Measure.js」を作成しました
var Measure = module.exports = function(param_timeout, param_cb) {
this.timeout = param_timeout;
this.cb = param_cb;
}
Measure.prototype = {
startDate: "0",
timeout:"0",
cb:null,
start : function() {
this.startDate = new Date();
console.log('started');
},
stop : function() {
var stopDate = new Date();
this.cb(null,(stopDate-this.startDate));
}
}
私はそれを次のコードで使用します:
var Measure = require('./Measure.js');
measure1 = new Measure(100,function(err,result){console.log('result: ' + result)});
measure1.start();
//do something
measure1.stop();
そしてそれはうまく機能します。ただし、これを試してみると:
var Measure = require('./Measure.js');
measure1 = new Measure(100,function(err,result){console.log('result: ' + result)});
measure1.start();
//do something
setTimeout(measure1.stop,100);
それは機能せず、TypeErrorをスローします:
TypeError: Object #<Object> has no method 'cb'
私のコードの何が問題になっていますか?