如何在node.js回调中维护范围?

Mah*_*nic 19 javascript node.js

我是一位经验丰富的软件开发人员,但对JS和节点来说都是新手.我不是超级嵌套代码的忠实粉丝,所以我一直试图将回调分解为自己的函数.虽然在回调触发时弄清楚如何保持范围,但我遇到了麻烦.我在周围读到,如果我在回调上创建一个闭包它可以工作,但它似乎不像我预期的那样工作.

这是一个非常简单的代码,对我来说不起作用:

function writeBody()
{
    res.end("<h1> Hooray! </h1>");
}

http.createServer(function(req, res)
{
    res.writeHead('Content-Type', 'text/html');
    setTimeout(function(){writeBody()}, 2000);
}).listen(8000);
Run Code Online (Sandbox Code Playgroud)

我认为通过在function()闭包中包装writeBody()调用,我会在超时后获得我需要的范围,但是当writeBody()触发时我得到了

ReferenceError:res未定义

谁能告诉我,我做错了什么?

Ivo*_*zel 37

基本上,不是闭包如何工作,函数继承其外部作用域,它是如何工作的.

// this function only inherits the global scope
function writeBody()
{
    res.end("<h1> Hooray! </h1>");
}

http.createServer(function(req, res) // a new local varaible res is created here for each callback
{
    res.writeHead('Content-Type', 'text/html');
    // annonymous function inheris both the global scope
    // as well as the scope of the server callback
    setTimeout(function(){

        // the local variable res is available here too
        writeBody()

    }, 2000);
}).listen(8000);
Run Code Online (Sandbox Code Playgroud)

为了使它工作,只需将res对象传递给函数,因为它在超时回调中可用.

function writeBody(res)
{
    // NOT the same variable res, but it holds the same value
    res.end("<h1> Hooray! </h1>");
}

http.createServer(function(req, res)
{
    res.writeHead('Content-Type', 'text/html');
    setTimeout(function(){
        writeBody(res); // just pass res
    }, 2000);
}).listen(8000);
Run Code Online (Sandbox Code Playgroud)

但是你需要注意这样的事情:

for(var i = 0; i < 10; i++) { // only one i gets created here!()
    setTimeout(function() {
        console.log(i); // this always references the same variable i
    }, 1000);
}
Run Code Online (Sandbox Code Playgroud)

这将打印10十次,因为引用是相同的并且i一直增加到10.如果你想拥有为每个变量创建一个新变量所需的不同数字,可以通过将其包装setTimeouti作为参数传递的匿名自我函数中,或者通过调用设置timouet和接收的其他方法来实现.在i作为参数.

// anoynmous function version
for(var i = 0; i < 10; i++) {
    (function(e){ // creates a new variable e for each call
        setTimeout(function() {
            console.log(e); 
        }, 1000);
    })(i); // pass in the value of i
}

// function call version
for(var i = 0; i < 10; i++) {
    createTimeoutFunction(i);
}
Run Code Online (Sandbox Code Playgroud)