Javascript nodejs express:将参数存储在变量中
我对nodejs还是新手,仍然不知道如何访问函数中的变量并回调该变量 检查几个链接:但我真的很困惑。请帮忙 不确定如何访问函数中的变量并回调该变量 以下是一些规则: 1.函数中声明的变量是函数的“局部”。 以下是函数的本地含义的示例:Javascript nodejs express:将参数存储在变量中,javascript,node.js,socket.io,Javascript,Node.js,Socket.io,我对nodejs还是新手,仍然不知道如何访问函数中的变量并回调该变量 检查几个链接:但我真的很困惑。请帮忙 不确定如何访问函数中的变量并回调该变量 以下是一些规则: 1.函数中声明的变量是函数的“局部”。 以下是函数的本地含义的示例: function do_stuff() { var x = 10; } do_stuff(); console.log(x); 在函数执行之前,x不存在 当函数完成执行时,x被销毁并不再存在 因此,行console.log(x)生成错误: Ref
function do_stuff() {
var x = 10;
}
do_stuff();
console.log(x);
console.log(x)代码>生成错误:
ReferenceError: x is not defined
在您的第二个链接中提出问题的人无法理解,一旦函数完成执行,局部变量就会被销毁,因此他们代码中的username变量不能在声明username变量的匿名函数之外的任何地方访问
另一方面,如果不使用var声明变量,则javascript会创建一个全局变量:
function do_stuff() {
x = 10;
}
do_stuff();
console.log(x); //=>10
这相当于写:
var x;
function do_stuff() {
x = 10;
}
do_stuff();
console.log(x); //=>10
通常认为函数在函数外部操纵变量的值是不好的做法。您应该选择以下选项:
function do_stuff() {
return 10;
}
var x = do_stuff();
在您的例子中,express将调用匿名函数,而express只会丢弃任何返回值。据推测,express的功能如下:
function express_get(func) {
console.log("express_get is executing");
requ = {greeting: 'hello'};
resp = {};
func(requ, resp); //Anything returned by func is not captured in a variable
}
//Here is an attempt to call express_get() and return
//the data rather than setting a global variable:
express_get(function(requ, resp) {
console.log("my callback is executing");
console.log(requ);
return {user_id: 1, room_id: 'A'};
});
--output:--
express_get is executing
my callback is executing
{ greeting: 'hello' }
function do_stuff() {
var x;
for (x=0; x < 10; ++x) {
console.log(x);
}
console.log(x) //=> 10
}
do_stuff();
^ part of inner's surrounding scope
|
|
function outer() {
^
| part of inner's surrounding scope
|
v
----------------------+
function inner() { |
|
} |
----------------------+
^
| part of inner's surrounding scope
v
--------------------------+
function other_inner { |
|
} |
--------------------------+
^
| part of inner's surrounding scope
|
V
}
|
|
V
part of inner's surrounding scope
function outer() {
var funcs = [];
var x;
for(x=0; x < 10; ++x) {
function inner() {
console.log(x);
}
funcs.push(inner);
}
return funcs;
}
因为express正在调用您的请求处理程序函数,并且因为express只是丢弃任何返回值,所以无法访问您从请求处理程序函数返回的值
也许你对这个问题的想法是错误的?与其试图找出如何将请求数据获取到io.on('connect')代码中,不如考虑如何在路由处理程序函数中获取io.on('connect')代码。如果您这样做会发生什么:
router.route(...).get(function(req,res){
user_id = req.param('user_id');
room_id = req.param('room_id');
io.on('connection', function(socket){
console.log ( user_id );
console.log ( room_id );
});
});
根据本答案中讨论的所有规则,您应该尝试确定:
传递给get()的匿名函数中的代码是否可以看到io变量
传递给io.on()的匿名函数中的代码能否看到user\u id和room\u id变量
2.函数的参数变量也是函数的局部变量。
以下是一个例子:
function do_stuff(x) {
console.log(x); //=>hello
}
do_stuff("hello");
console.log(x); //=>ReferenceError: x is not defined
function do_stuff() {
for (var x=0; x < 10; ++x) {
console.log(x);
}
console.log(x) //=>10
}
do_stuff();
var x = 10;
function do_stuff() {
console.log(x);
}
do_stuff(); //=>10
因此,x在函数执行之前不存在,而x在函数执行完毕后就不再存在
3.在for循环中声明的变量不是for循环的局部变量。
以下是一个例子:
function do_stuff(x) {
console.log(x); //=>hello
}
do_stuff("hello");
console.log(x); //=>ReferenceError: x is not defined
function do_stuff() {
for (var x=0; x < 10; ++x) {
console.log(x);
}
console.log(x) //=>10
}
do_stuff();
var x = 10;
function do_stuff() {
console.log(x);
}
do_stuff(); //=>10
很简单。周边范围是指:
//First line of program
^
|
| part of do_stuff() surrounding scope
|
v
------------------------+
function do_stuff() { |
//Do stuff |
} |
------------------------+
^
| part of do_stuff() surrounding scope
|
v
---------------------------+
function other_func() { |
|
|
//Do other stuff |
|
} |
---------------------------+
^
| part of do_stuff surrounding scope
|
v
//Last line of program
请注意,other_func内的代码区域不属于do_stuff的周围范围,因此do_stuff内的代码无法看到other_func内声明的变量
对于嵌套函数,周围的作用域如下所示:
function express_get(func) {
console.log("express_get is executing");
requ = {greeting: 'hello'};
resp = {};
func(requ, resp); //Anything returned by func is not captured in a variable
}
//Here is an attempt to call express_get() and return
//the data rather than setting a global variable:
express_get(function(requ, resp) {
console.log("my callback is executing");
console.log(requ);
return {user_id: 1, room_id: 'A'};
});
--output:--
express_get is executing
my callback is executing
{ greeting: 'hello' }
function do_stuff() {
var x;
for (x=0; x < 10; ++x) {
console.log(x);
}
console.log(x) //=> 10
}
do_stuff();
^ part of inner's surrounding scope
|
|
function outer() {
^
| part of inner's surrounding scope
|
v
----------------------+
function inner() { |
|
} |
----------------------+
^
| part of inner's surrounding scope
v
--------------------------+
function other_inner { |
|
} |
--------------------------+
^
| part of inner's surrounding scope
|
V
}
|
|
V
part of inner's surrounding scope
function outer() {
var funcs = [];
var x;
for(x=0; x < 10; ++x) {
function inner() {
console.log(x);
}
funcs.push(inner);
}
return funcs;
}
以下是一个例子:
var x = 10;
function outer() {
function inner() {
console.log(x);
}
inner();
}
outer(); //=>10
对于定义为函数参数的匿名函数,例如:
some_func(10, 20, function(req, resp) {.....});
这与写这篇文章几乎是一样的:
function request_handler(req, resp) {
//Do stuff
}
some_func(10, 20, requestHandler);
您可以使用上面的一个图来了解request_handler函数的周围范围
在计算机科学行话中,javascript函数被称为闭包,javascript函数被认为是在定义函数时存在的周围范围内的变量上关闭的。如果您正在阅读一些声明某个函数是闭包的内容,您可以低声对自己说,“当函数执行时,它可以看到在定义函数时存在的周围范围中的变量。”
下面是一个更复杂的示例:
function outer() {
var x = 10;
function inner() {
console.log(x);
}
return inner;
}
func = outer();
//outer() has finished executing here.
func();
根据规则1,您可能期望在outer()完成执行后,x将被销毁,因此,当inner()通过func()执行时,您将得到一个未定义的错误。然而,情况并非如此。内部()仍然可以看到x,即使通常在外部()完成执行时x会被销毁。internal()表示在定义internal时关闭周围范围中存在的变量
下面是另一个例子:
function outer() {
var x = 10;
function inner() {
console.log(x);
}
return inner;
}
func = outer();
//outer() has finished executing here.
var x = 20; //New code
func();
那里的输出是什么?20? 不。函数在定义inner()时查看周围作用域中的变量,而不是在执行inner时查看周围作用域中的变量
最后一个例子:
function outer() {
var funcs = [];
for(var x=0; x < 10; ++x) {
function inner() {
console.log(x);
}
funcs.push(inner);
}
return funcs;
}
ten_funcs = outer();
for(var y=0; y < 10; ++y) {
ten_funcs[y]();
}
这是规则4的一个例外。在函数内部,如果您声明的变量与函数周围作用域中的变量同名,javascript将创建一个新的局部变量,该变量将隐藏在周围作用域中。非常感谢您的解释!这将进一步阐明全局变量。但是在我的例子中,我需要用一个函数包装路由器吗function express_get(funct){router.route().get(function(){}}
?@tonoslfx,对不起,我很忙,没有时间为您的问题提出解决方案。请参见规则1末尾添加的文本。