Javascript在函数问题之外使用变量

时间:2023-01-06 00:41:46

I am currently learning node.js and already meet several times the same problem that seems very simple but I can't still understand how to solve it.

我目前正在学习node.js,并且已经遇到过几次相同的问题,看起来非常简单,但我仍然无法理解如何解决它。

Code:

码:

var SRC_PORT = 6025;
var dgram = require('dgram');
var clientUDP = dgram.createSocket("udp4");
var test

clientUDP.bind(SRC_PORT, function () {
    multicastNew()
});

function multicastNew() {
    var test = 777
    console.log(test);
}

Problem Cant use variable test content outside the function multicastNew()

问题不能在函数multicastNew()之外使用变量测试内容

In the function multicastNew() I have a variable var test. In that function multicastNew() I gave to test = 777. When I want to console.log(test) in the same function multicastNew() everything works,it outputs 777. The problem is that when I want to console.log(test) outside function multicastNew() it outputs undefined.

在函数multicastNew()中,我有一个变量var test。在那个函数multicastNew()我给test = 777.当我想在同一个函数中使用console.log(test)时,一切正常,它输出777.问题是当我想要console.log时(测试) )outside函数multicastNew()它输出undefined。

Can you please explain me how to solve this issue and why it is. Thank you!

能否请您解释一下如何解决这个问题及其原因。谢谢!

2 个解决方案

#1


2  

You should change var test = 777; to test = 777; in mulitcastNew(). Your code should be as such:

你应该改变var test = 777;测试= 777;在mulitcastNew()中。您的代码应该是这样的:

var SRC_PORT = 6025;
var dgram = require('dgram');
var clientUDP = dgram.createSocket("udp4");
var test;

clientUDP.bind(SRC_PORT, function () {
    multicastNew();
});

function multicastNew() {
    test = 777;
    console.log(test);
}

A note on scope. In Javascript, functions create scope. Any variable defined with var inside of a function is a local variable, invisible outside the function. It's only local to the wrapping function, and if there is no wrapping function, it becomes a global variable.

关于范围的说明。在Javascript中,函数创建范围。在函数内部使用var定义的任何变量都是局部变量,在函数外部是不可见的。它只是包装函数的本地,如果没有包装函数,它就变成了一个全局变量。

Consider the following:

考虑以下:

//this is global scope
var a = "Chris";
var b = "Inspired";

function nameChange(){
    var a = "inspired"; // local to the function nameChange()
    b = "Chris"; //without var we are changing the global variable
    console.log(a); //will output inspired
}
nameChange(); // inspired
console.log(a); // Chris
console.log(b); // Chris

#2


1  

It's all about function scope. When u declare var test = 777, then u are creating new variable in scope of your function multicastNew(). This variable covers your 'main' variable from the global scope... So your function works from now on your local variable, not on the one from the global scope.
JavaScript always look for variables inside scope it is called. In your example, when u try to call test outside the multicastNew(), then current scope is GLOBAL, so it finds your var test from the begining of your code. It's always work from inside to outside (closures).

You can read:
Scopes

一切都与功能范围有关。当你声明var test = 777时,你正在你的函数multicastNew()的范围内创建新的变量。这个变量覆盖了全局范围内的'main'变量...所以你的函数从现在开始工作在你的局部变量上,而不是全局范围的变量。 JavaScript总是在调用范围内查找变量。在您的示例中,当您尝试在multicastNew()之外调用test时,则当前作用域为GLOBAL,因此它会从代码的开头找到您的var测试。它总是从内到外(闭合)工作。你可以阅读:范围

#1


2  

You should change var test = 777; to test = 777; in mulitcastNew(). Your code should be as such:

你应该改变var test = 777;测试= 777;在mulitcastNew()中。您的代码应该是这样的:

var SRC_PORT = 6025;
var dgram = require('dgram');
var clientUDP = dgram.createSocket("udp4");
var test;

clientUDP.bind(SRC_PORT, function () {
    multicastNew();
});

function multicastNew() {
    test = 777;
    console.log(test);
}

A note on scope. In Javascript, functions create scope. Any variable defined with var inside of a function is a local variable, invisible outside the function. It's only local to the wrapping function, and if there is no wrapping function, it becomes a global variable.

关于范围的说明。在Javascript中,函数创建范围。在函数内部使用var定义的任何变量都是局部变量,在函数外部是不可见的。它只是包装函数的本地,如果没有包装函数,它就变成了一个全局变量。

Consider the following:

考虑以下:

//this is global scope
var a = "Chris";
var b = "Inspired";

function nameChange(){
    var a = "inspired"; // local to the function nameChange()
    b = "Chris"; //without var we are changing the global variable
    console.log(a); //will output inspired
}
nameChange(); // inspired
console.log(a); // Chris
console.log(b); // Chris

#2


1  

It's all about function scope. When u declare var test = 777, then u are creating new variable in scope of your function multicastNew(). This variable covers your 'main' variable from the global scope... So your function works from now on your local variable, not on the one from the global scope.
JavaScript always look for variables inside scope it is called. In your example, when u try to call test outside the multicastNew(), then current scope is GLOBAL, so it finds your var test from the begining of your code. It's always work from inside to outside (closures).

You can read:
Scopes

一切都与功能范围有关。当你声明var test = 777时,你正在你的函数multicastNew()的范围内创建新的变量。这个变量覆盖了全局范围内的'main'变量...所以你的函数从现在开始工作在你的局部变量上,而不是全局范围的变量。 JavaScript总是在调用范围内查找变量。在您的示例中,当您尝试在multicastNew()之外调用test时,则当前作用域为GLOBAL,因此它会从代码的开头找到您的var测试。它总是从内到外(闭合)工作。你可以阅读:范围