waltheri / wgo.js

JavaScript library for game of Go.
http://wgo.waltheri.net/
306 stars 127 forks source link

board.removeEventListener is not workly #65

Closed lifengbo closed 8 years ago

lifengbo commented 8 years ago

When I use the board's method removeEventListener is seemly not work, like that:

    // function handling board clicks in normal mode
    var board_click_default = function(x,y) {
        // do somethis
        console.log("==x: " + x + "--y: " + y);
    }
        board.addEventListener("click", board_click_default.bind(player));
    board.removeEventListener("click", board_click_default.bind(player));
but the board's last click listener is not remove.
waltheri commented 8 years ago

The problem with your code is, that method bind() creates a new function, so you are removing event listener which isn't there. Just try to put

board_click_default.bind(player) == board_click_default.bind(player)

into the console and you will get false. So correct code should look like this:

var board_click_default = function(x,y) {
    // do somethis
    console.log("==x: " + x + "--y: " + y);
}.bind(player);

board.addEventListener("click", board_click_default);
board.removeEventListener("click", board_click_default);