1

Let's say I have a javascript function as follows:

car.prototype = {
    this.stolen = "",

    initialize: function(){
        this.stolen = false;
    },

    steal: function(){
        Event.observe(window, "resize", function(){
            this.stolen = true;
        });
    }
}

In the steal method, how can I refer to the stolen property of the car object within the Event.observe() method? In the code above, this in the Event.observe() method is referring to the window instead of the car object.

SK.
  • 4,174
  • 4
  • 30
  • 48
Vee
  • 1,821
  • 3
  • 36
  • 60

1 Answers1

3

You bind the function:

Event.observe(window, "resize", (function(){
    this.stolen = true;
}).bind(this));
Victor Stanciu
  • 12,037
  • 3
  • 26
  • 34