![](/img/trans.png)
[英]Why doesn't Javascript map function String.toLowerCase doesn't work in nodejs 6?
[英]nodejs function hoisting : why it doesn't work?
这有效:
var http = require('http');
var handler = function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World!');
}
http.createServer(handler).listen(8080);
但这不是
var http = require('http');
http.createServer(handler).listen(8080);
var handler = function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World!');
}
我不明白为什么会这样,因为它应该继续吊装,所以我没有出错。
那不是功能提升,那是可变提升 。 等效于:
var http = require('http');
var handler;
http.createServer(handler).listen(8080);
handler = function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World!');
}
函数提升仅适用于函数声明 (上面是一个函数表达式 ):
var http = require('http');
http.createServer(handler).listen(8080);
function handler(req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World!');
}
更多信息: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/function#Function_declaration_hoisting
var http = require('http');
http.createServer(handler).listen(8080);
var handler = function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World!');
}
在这种情况下,声明的函数尚不存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.