麻烦与javascript中的变量范围

问题描述 投票:1回答:3

我是javascript的新手,我在计数程序方面遇到了麻烦,我相信这可能是变量范围的麻烦。

var count = 0; {
    function gimmeRandom() {
        var rand = Math.floor(Math.random() * 10) + 1;
        count++;
    }

    function countToRandom() {
        for (count = 1; count <= rand; count++) {
            console.log(count);
        }
    }

    console.log("Counting to a random number");
    gimmeRandom();
    countToRandom();
    console.log("Counting to another random number");
    gimmeRandom();
    countToRandom();
    console.log("There has been " + count + " random numbers used");
}
javascript counting
3个回答
1
投票

你在var rand中声明了gimmeRandom,你无法在countToRandom中访问它。您可能想要一个全局变量,就像使用count一样。


0
投票

将函数范围内的var声明为该函数的变量。你有两个选择。

  1. 由于您首先调用gimmeRandom,因此请删除var关键字,它将自动为全局关键字。

var count = 0;     

{                                               
function gimmeRandom()                                              
{                                               
  rand = Math.floor(Math.random()*10)+1;                                                
  count++;                                              
}                                               

function countToRandom()                                                
{                                               
  for (count = 1; count <= rand; count++)                                               
  {                                             
     console.log(count);                                                
  }                                             
}                                               

console.log("Counting to a random number");                                             
gimmeRandom();                                              
countToRandom();                                                
console.log("Counting to another random number");                                               
gimmeRandom();                                              
countToRandom();                                                
console.log("There has been "+count+" random numbers used");                                                
}

  1. 在页面顶部定义var rand以使其成为全局变量。

var count = 0;     
var rand = 0;

{                                               
function gimmeRandom()                                              
{                                               
  rand = Math.floor(Math.random()*10)+1;                                                
  count++;                                              
}                                               

function countToRandom()                                                
{                                               
  for (count = 1; count <= rand; count++)                                               
  {                                             
     console.log(count);                                                
  }                                             
}                                               

console.log("Counting to a random number");                                             
gimmeRandom();                                              
countToRandom();                                                
console.log("Counting to another random number");                                               
gimmeRandom();                                              
countToRandom();                                                
console.log("There has been "+count+" random numbers used");                                                
}

0
投票

您可以在此函数中定义rand:

    function gimmeRandom() {
         var rand = Math.floor(Math.random() * 10) + 1;
         count++;
    }

然后尝试在另一个函数中使用它:

    function countToRandom() {
        for (count = 1; count <= rand; count++) {
            console.log(count);
        }
    }

如这个问题所示:

What is the scope of variables in JavaScript?

您定义的变量rand被赋予gimmeRandom()函数的局部范围,因此不能在该函数之外使用。

要在函数中使用它,您可能希望使变量具有全局范围

© www.soinside.com 2019 - 2024. All rights reserved.