Trying to simplify some Javascript with closures
- by mvalente
Hi, I'm trying to simplify some JS code that uses closures but I am getting nowhere (probably because I'm not grokking closures)
I have some code that looks like this:
var server = http.createServer(function (request, response) {
var httpmethods = {
"GET": function() {
alert('GET')
},
"PUT": function() {
alert('PUT')
}
};
});
And I'm trying to simplify it in this way:
var server = http.createServer(function (request, response) {
var httpmethods = {
"GET": function() {
alertGET()
},
"PUT": function() {
alertPUT()
}
};
});
function alertGET() {
alert('GET');
}
function alertPUT() {
alert('PUT');
}
Unfortunately that doesnt seem to work...
Thus:
- what am I doing wrong?
- is it possible to do this?
- how?
TIA
-- MV