如何从内部函数访问函数参数?

时间:2013-04-20 17:13:44

标签: javascript node.js

如何访问函数内的响应参数.exec(function(err,repl){?是否可以不创建局部变量?

#!/usr/local/bin/node

// Load the http module to create an http server.
var http = require('http');
var redis = require('redis');
var client = redis.createClient();


// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  client.multi([
    ['get', 'a'],
    ['get', 'b']
  ]).exec(function(err, repl) {
    console.log(err);
    console.log(repl[0]);
    response.end('Repl: ' + repl[0].toString());
  });
});

// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8000);

// Put a friendly message on the terminal
console.log("Server running at http://127.0.0.1:8000/");

1 个答案:

答案 0 :(得分:1)

只需访问它。参数被自动视为局部变量,内部函数可以访问其包含函数的局部变量(除非它使用自己的同名变量来隐藏它们)。