Search code examples
javascriptprototypejsthis

problems with 'this' in OOP JS


I am making a game and using Prototype JS framework for an easy work with objects. I made my own methods for non DOM events among my own objects.

StageObject is the child of Screen object and it's method load() loads the image of the object and as soon as it's loaded fires the event. Here it is:

var StageObject = Class.create(Screen, {...}) 

StageObject.addMethods({
    /*.....*/
    load:function(){
        this.img = new Image();
        this.img.src = "./src/img/"+this.src;        
        this.img.onload = this.objectLoadedHandler();                           
    }, 
}) 

objectLoadedHandler belongs to StageObject. It fires another event, whitch handler is in Screen object, not in StageObject. The problem is that inside of this second handler 'this' does not point at the current object (Screen). I have not figured out what it points at, but I can not reach the proberties of Screen object which I need for further calculations. Please give me a piece of advice about that.


Solution

  • this.img.onload = this.objectLoadedHandler.bind(this);