TypeError:不是构造函数

时间:2017-04-12 10:50:10

标签: javascript prototype

我正在使用tracking.js,我想创建customTracker所以:

var CustomTracker = function(){
  CustomTracker(this, 'constructor');
}

CustomTracker.prototype.track = function(pixel, width, height){

  var results = ['foo'];

  this.emit('track', {
      data: results
  });
}

tracking.inherits(CustomTracker, tracking.Tracker);

var myTracker = new tracking.CustomTracker

myTracker.on('track', function(event) {
  console.log('Event: ', event);
});

tracking.track('#myVideo', myTracker);

但是控制台说:

  

未捕获TypeError:tracking.CustomTracker不是构造函数

问题出在哪里。我不明白为什么?

1 个答案:

答案 0 :(得分:0)

试试这个(来自文档):

var MyTracker = function() {
    MyTracker(this, 'constructor');
}

tracking.inherits(MyTracker, tracking.Tracker);

var MyTracker = function() {
    MyTracker.prototype.track = function(pixels, width, height) {
        // Your code here

        this.emit('track', {
            // Your results here
        });
    }
}

var myTracker = new tracking.MyTracker();

myTracker.on('track', function(event) {
    // Results are inside the event
});

tracking.track('#myVideo', myTracker);