Skip to main content
added 11 characters in body; edited tags; edited title
Source Link

OOP in javascriptJavascript with callbacks

I want to close my window (just a div with position absolute that is draggable) when I click on the close link

This is my code:

function ApplicationWindow() {
    this.window = $('<div class="window"></div>');
    
    this.create = function create() {
        //.....
        var closeButton = this.window.find('.close');
        closeButton.live('click', this.close);
    }

    this.close = function close() {
        this.window.fadeOut(200);
    };
}

When I click on the close button the close function is executed, but the problem is that I get an error "this.window is undefined".:

"this.window is undefined".

That is because the close function is passed as a callback I think, but my question is how I can solve this on a nice way?

OOP in javascript with callbacks

I want to close my window (just a div with position absolute that is draggable) when I click on the close link

This is my code

function ApplicationWindow() {
    this.window = $('<div class="window"></div>');
    
    this.create = function create() {
        //.....
        var closeButton = this.window.find('.close');
        closeButton.live('click', this.close);
    }

    this.close = function close() {
        this.window.fadeOut(200);
    };
}

When I click on the close button the close function is executed, but the problem is that I get an error "this.window is undefined". That is because the close function is passed as a callback I think, but my question is how I can solve this on a nice way?

OOP in Javascript with callbacks

I want to close my window (just a div with position absolute that is draggable) when I click on the close link

This is my code:

function ApplicationWindow() {
    this.window = $('<div class="window"></div>');
    
    this.create = function create() {
        //.....
        var closeButton = this.window.find('.close');
        closeButton.live('click', this.close);
    }

    this.close = function close() {
        this.window.fadeOut(200);
    };
}

When I click on the close button the close function is executed, but the problem is that I get an error:

"this.window is undefined".

That is because the close function is passed as a callback I think, but my question is how I can solve this on a nice way?

Source Link
Derk
  • 181
  • 2
  • 2
  • 6

OOP in javascript with callbacks

I want to close my window (just a div with position absolute that is draggable) when I click on the close link

This is my code

function ApplicationWindow() {
    this.window = $('<div class="window"></div>');
    
    this.create = function create() {
        //.....
        var closeButton = this.window.find('.close');
        closeButton.live('click', this.close);
    }

    this.close = function close() {
        this.window.fadeOut(200);
    };
}

When I click on the close button the close function is executed, but the problem is that I get an error "this.window is undefined". That is because the close function is passed as a callback I think, but my question is how I can solve this on a nice way?