Uncaught TypeError: is not a function

Can somebody please explain what I am doing wrong here?

OrbitCamera.prototype.tweenCamera = function () {
 tween.on('complete', function () {
        console.log('tween completed');
        this.test();
    });
};

OrbitCamera.prototype.test = function () { 
     console.log('um, actually this is a function');
};

I excluded the tween start etc and it all works and it logs “tween completed”, but no matter which function I call after that I keep getting above error and I have no idea why.

The this in the closure of the tween.on function is in a different scope. (https://stackoverflow.com/questions/346015/javascript-closures-and-this)

Easy fix is to assign this to something else:

OrbitCamera.prototype.tweenCamera = function () {
    var self = this;
    tween.on('complete', function () {
        console.log('tween completed');
        self.test();
    });
};

OrbitCamera.prototype.test = function () { 
     console.log('um, actually this is a function');
};
1 Like

Thanks that worked! Still new to JS so getting used to things like that.