In the dummy example code below, I have created a class (WindowResizer) that listens to the browser window when it get resized, 250 ms after the window is resized a timer calls a method (timerAlarm) that belongs to the WindowResizer class. But the 'this' variable in this method is the window object. Why? And what should I do to reach the instance of my WindowResizer?
<script>
$(document).ready(function () {
var windowResizer = new WindowResizer();
windowResizer.name = "Tester";
windowResizer.window = $(window);
windowResizer.attachEvents();
});
function WindowResizer(window) {
this.window = window;
}
WindowResizer.prototype.attachEvents = function () {
var self = this;
self.window.resize(function () {
clearTimeout(self.windowResizeTimer);
self.windowResizeTimer = setTimeout(self.timerAlarm, 250);
});
};
WindowResizer.prototype.timerAlarm = function () {
// Here the variable this is the window, but I want it to be the instance of WindowResizer that I created in the document ready-function, why!?
console.log(this);
}
</script>