I have a pseudo-class in javascript that has a method to add and remove listeners to two buttons.
This is the code:
function FirstObj(secondObj){
this.loginButton = document.getElementById("login");
this.logoutButton = document.getElementById("logout");
this.secondObj = secondObj
}
FirstObj.prototype = {
manageListeners : function(state){ //state is a boolean
var self = this;
if (state) {
display += "none";
this.loginButton.addEventListener("click", function(){
self.seconfObj.makeSomething();
}, false);
this.logoutButton.removeEventListener("click", /*???*/ , false);
}
else {
this.logoutButton.addEventListener("click", function(){
self.logout();
}, false);
this.loginButton.removeEventListener("click", /*???*/ , false);
}
},
logout : function(){
//logout...
}
}
the question is: how i could modify this code to manage event listener correctly?
I don't think you can remove an event listener for an anonymous function. Use named functions instead:
var myEventListener = function() {
console.log("Hello World!");
};
myElement.addEventListener("click", myEventListener, false);
myElement.removeEventListener("click", myEventListener, false);
Here is a slightly modified version of your class that should manage the event listeners properly:
function FirstObj(secondObj){
var self = this;
this.loginButton = document.getElementById("login");
this.logoutButton = document.getElementById("logout");
this.secondObj = secondObj;
this.loginButtonClicked = function(){
self.secondObj.makeSomething();
};
this.logoutButtonClicked = function(){
self.logout();
};
}
FirstObj.prototype = {
manageListeners : function(state){
if (state) {
display += "none";
this.loginButton.addEventListener("click", this.loginButtonClicked, false);
this.logoutButton.removeEventListener("click", this.logoutButtonClicked, false);
}
else {
this.logoutButton.addEventListener("click", this.logoutButtonClicked, false);
this.loginButton.removeEventListener("click", this.loginButtonClicked, false);
}
},
logout : function(){
// Log out...
}
};
If you rewrite your class to take advantage of closures, though, you can simplify it to this:
function FirstObj(secondObj){
var self = this;
var loginButton = document.getElementById("login");
var logoutButton = document.getElementById("logout");
var loginButtonClicked = function(){
secondObj.makeSomething();
};
var logoutButtonClicked = function(){
self.logout();
};
this.manageListeners = function(state){
if (state) {
display += "none";
loginButton.addEventListener("click", loginButtonClicked, false);
logoutButton.removeEventListener("click", logoutButtonClicked, false);
}
else {
logoutButton.addEventListener("click", logoutButtonClicked, false);
loginButton.removeEventListener("click", loginButtonClicked, false);
}
};
this.logout = function(){
// Log out...
};
}
Here I've assumed that loginButton
, logoutButton
, and secondObj
don't need to be accessed from outside the class. If they do, just make them properties of FirstObj
and update the code that references them (using this
in scope and self
out of scope).