Trying to simplify some Javascript with closures
Posted
by mvalente
on Stack Overflow
See other posts from Stack Overflow
or by mvalente
Published on 2010-05-15T15:33:46Z
Indexed on
2010/05/15
15:44 UTC
Read the original article
Hit count: 212
JavaScript
|closures
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
© Stack Overflow or respective owner