I am trying to understand some code used to make a Node.js dispatcher but I can't understand one line. Maybe my JavaScript gap . . . I commented the code with my doubts.
var HttpDispatcher = function() {
this.listeners = { get: [ ], post: [ ] };
}
HttpDispatcher.prototype.on = function(method, url, cb) {
this.listeners[method].push({
cb: cb,
url: url
});
}
HttpDispatcher.prototype.onGet = function(url, cb) {
this.on('get', url, cb);
}
HttpDispatcher.prototype.onPost = function(url, cb) {
this.on('post', url, cb);
}
HttpDispatcher.prototype.dispatch = function(req, res) {
var parsedUrl = require('url').parse(req.url, true);
var method = req.method.toLowerCase();
this.listener[method][parsedUrl.pathname](req, res); // i don't understand this line
}
Why do we refer to this.listener as a bidimensional array? We defined listeners like an array of object! and why do we pass parameters?