Problem:
When I try to call an object method in this particular way in JavaScript, I get the following error:
TypeError: listener.update is not a function
My code:
<html>
<head>
<script src="library.js"></script>
</head>
<body>
<script>
// manages listeners etc.
function Model() {
var listeners = [];
this.addListener = function(listener) {
listeners.push(listener);
};
// the model will call the listeners when "setting the selection"
this.setSelection = function() {
for (listener in listeners)
listener.update();
};
};
// test function that will be used as update
function moveon() {
var answer = confirm("Ready to move on?");
if (answer) window.location = "http://google.com";
}
// create a model and add a listener
var model = new Model();
var listnr = {};
listnr.update = moveon;
model.addListener(listnr);
// update listener
setTimeout(model.setSelection, 2000); // this doesn't work
// setTimeout(listnr.update, 2000); // but this does
</script>
</body>
</html>
Explanation of the code:
The Model object manages a list of listeners, and calls their update method when some state has changed. In my example, this happens when setSelection is called.
Note:
The error isn't very insightful, and, if I uncomment the last line, listnr.update works fine.
Question:
Why do I get this error when the method is called from the model and/or how can I solve this issue?