在javascript中使用全局变量

时间:2022-07-02 16:49:00

How do I do this?

我该怎么做呢?

My code is something like this:

我的代码是这样的:

var number = null;

function playSong(artist,title,song,id)
{
    alert('old number was: '+[number]+'');


    var number = '10';

    alert(''+[number]+'');
}

The first alert always returns 'old number was: ' and not 10. Shouldn't it return 10 on both alerts on the second function call?

第一个警报总是返回'old number was:'而不是10.在第二个函数调用时,它不应该在两个警报上都返回10吗?

6 个解决方案

#1


By using var when setting number = '10', you are declaring number as a local variable each time. Try this:

通过在设置number ='10'时使用var,您每次都将数字声明为局部变量。试试这个:

var number = null;

function playSong(artist,title,song,id)
{
    alert('old number was: '+[number]+'');


    number = '10';

    alert(''+[number]+'');
}

#2


Remove the var in front of number in your function. You are creating a local variable by

删除函数中数字前面的var。您正在创建一个局部变量

var number = 10;

what you need is just

你需要的只是

number = 10;

#3


The problem is that you're declaring a new variable named number inside of the function. This new variable hides the global number variable, so the line number = 10 assigns only to this new local variable.

问题是你在函数内部声明了一个名为number的新变量。此新变量隐藏全局数字变量,因此行号= 10仅分配给此新的局部变量。

You need to remove the var keyword from var number = 10.

您需要从var number = 10中删除var关键字。

#4


Like in C, you need to define your variable outside of the function/method to make it global.

与在C中一样,您需要在函数/方法之外定义变量以使其成为全局变量。

var number = 0;

function playSong(artist,title,song,id)
{
    alert('old number was: '+[number]+'');
    number = '10';
    alert(''+[number]+'');
}

#5


You can also access it in any function like window.number, after removing var inside function.

在删除var inside函数后,您还可以在window.number等任何函数中访问它。

#6


Let me explain in detail, To declaring global variable and local variable in javascript

让我详细解释一下,在javascript中声明全局变量和局部变量

var firstNumber=5;//local variable
secondNumber=10; //global variable or window object

When your program is like this

当你的程序是这样的

var number =1;
function playSong() { 
    alert(number);
    var number =2;
    alert(number);
}

As per the JavaScript compiler all Declaration/initialization of variable will move to top this concept is called Hoisting. Link : https://www.w3schools.com/js/js_hoisting.asp

根据JavaScript编译器的所有声明/初始化变量将移至顶部这个概念称为Hoisting。链接:https://www.w3schools.com/js/js_hoisting.asp

As per the compiler program will execute like

按照编译器程序执行就好了

var number; //Declaration will move to top always in Javascript
number=1;
function playSong() { 
    var number;
    alert(number); //output : undefied - This is local variable inside the function
    number =2;
    alert(number); // output : 2
}

If you need to access the global variable inside the function use window.number

如果需要访问函数内部的全局变量,请使用window.number

var number =1;
function playSong() { 
   var number =2;
   alert(window.number); // output : 1   -From Global variable
   alert(number); // output : 2   -From local variable
}

#1


By using var when setting number = '10', you are declaring number as a local variable each time. Try this:

通过在设置number ='10'时使用var,您每次都将数字声明为局部变量。试试这个:

var number = null;

function playSong(artist,title,song,id)
{
    alert('old number was: '+[number]+'');


    number = '10';

    alert(''+[number]+'');
}

#2


Remove the var in front of number in your function. You are creating a local variable by

删除函数中数字前面的var。您正在创建一个局部变量

var number = 10;

what you need is just

你需要的只是

number = 10;

#3


The problem is that you're declaring a new variable named number inside of the function. This new variable hides the global number variable, so the line number = 10 assigns only to this new local variable.

问题是你在函数内部声明了一个名为number的新变量。此新变量隐藏全局数字变量,因此行号= 10仅分配给此新的局部变量。

You need to remove the var keyword from var number = 10.

您需要从var number = 10中删除var关键字。

#4


Like in C, you need to define your variable outside of the function/method to make it global.

与在C中一样,您需要在函数/方法之外定义变量以使其成为全局变量。

var number = 0;

function playSong(artist,title,song,id)
{
    alert('old number was: '+[number]+'');
    number = '10';
    alert(''+[number]+'');
}

#5


You can also access it in any function like window.number, after removing var inside function.

在删除var inside函数后,您还可以在window.number等任何函数中访问它。

#6


Let me explain in detail, To declaring global variable and local variable in javascript

让我详细解释一下,在javascript中声明全局变量和局部变量

var firstNumber=5;//local variable
secondNumber=10; //global variable or window object

When your program is like this

当你的程序是这样的

var number =1;
function playSong() { 
    alert(number);
    var number =2;
    alert(number);
}

As per the JavaScript compiler all Declaration/initialization of variable will move to top this concept is called Hoisting. Link : https://www.w3schools.com/js/js_hoisting.asp

根据JavaScript编译器的所有声明/初始化变量将移至顶部这个概念称为Hoisting。链接:https://www.w3schools.com/js/js_hoisting.asp

As per the compiler program will execute like

按照编译器程序执行就好了

var number; //Declaration will move to top always in Javascript
number=1;
function playSong() { 
    var number;
    alert(number); //output : undefied - This is local variable inside the function
    number =2;
    alert(number); // output : 2
}

If you need to access the global variable inside the function use window.number

如果需要访问函数内部的全局变量,请使用window.number

var number =1;
function playSong() { 
   var number =2;
   alert(window.number); // output : 1   -From Global variable
   alert(number); // output : 2   -From local variable
}