【问题标题】:I'm having trouble understanding this Javascript function我无法理解这个 Javascript 函数
【发布时间】:2014-01-06 07:18:27
【问题描述】:

我不明白toAlert 变量在这里是如何工作的。为什么要给它分配两个引号?我也不明白 for 循环块中的“toAlert”语句。为什么 toAlert = toAlert?

在弄乱了函数之后,我想看看变量 toAlert 如果我要更改它的效果。所以我把它分配给

var toAlert;

它只提醒一行文本,而不是5。谁能给我解释一下?

var runAway = function(){
  var toAlert = "";
    for(var i = 0; i<5; i++){
      toAlert = toAlert + "Lions, Tigers, and Bears, Oh My!!\n";
    }
    alert(toAlert);
  }
}

runAway();

【问题讨论】:

  • 买一本javascript书会很有帮助。这是非常基本的事情。发生的情况是变量 toAlert 多次与另一个字符串连接,而您只“警告”一次。
  • 你在 runAway(); 之前有额外的右花括号 '}';
  • 3 个右括号只有 2 个左括号
  • "它只提醒一行文本":不,它没有!如果您没有将其分配给空字符串,它将警告未定义的 5 行

标签: javascript


【解决方案1】:
var toAlert = "";

这是一个空字符串。起初,toAlert 变量只是一个空字符串。

toAlert = toAlert + "Lions, Tigers, and Bears, Oh My!!\n";

您将"Lions, Tigers, and Bears, Oh My!!\n" 附加到toAlert 变量的前一个值。

toAlert += "Lions, Tigers, and Bears, Oh My!!\n";

你可以这样写。

【讨论】:

    【解决方案2】:

    var toAlert = "" ;表示toAlert是string类型的变量

    toAlert = toAlert + "Lions, Tigers, and Bears, Oh My!!\n";

    是将字符串“Lions, Tigers, and Bears, Oh My!!\n”连接到toAlert的值

    for循环内,

    第一次,toAlert 为空,所以在执行语句 toAlert = toAlert + "Lions, Tigers, and Bears, Oh My!!\n"; 后,toAlert 的值为

    ""+"狮子、老虎和熊,天哪!!\n"

    因为 + 在字符串的情况下表现得像一个连接运算符,它连接两个“字符串”

    第二次了

    “狮子、老虎和熊,哦,我的!!\n”+“狮子、老虎和熊,哦,我的!!\n”

    如果您需要将字符串附加 5 次,您的代码将是

    var runAway = function(){
        var toAlert = "";
        for(var i = 0; i<5; i++)
        {
            toAlert = toAlert + "Lions, Tigers, and Bears, Oh My!!\n";
        }
        alert(toAlert);
    }
    
    runAway();
    

    【讨论】:

    • @EmilioGort:不,他不是
    【解决方案3】:
        var runAway = function(){
        var toAlert = "";         // 1. this is just an empty string. Probably so that that they can customize it to how they want when they call alert(toAlert)
          for(var i = 0; i<5; i++){
            toAlert = toAlert + "Lions, Tigers, and Bears, Oh My!!\n";
          }
          alert(toAlert);
        }
        }
    
    runAway();
    
    1. 例如,编码员可以调用alert("This is error #424343 in div id #cats"); 因此,如果发生该错误,它将显示在警报中。 然后,如果 div id #dogs 中有错误,编码员会显示另一条警报/错误消息。所以他会输入alert("This is error whatever, in div id #dogs"); 来调用该特定事件的自定义错误消息。

    【讨论】:

      【解决方案4】:
        var runAway = function(){
         var toAlert = "";
         for(var i = 0; i<5; i++){
            toAlert = toAlert + "Lions, Tigers, and Bears, Oh My!!\n";
         }
         alert(toAlert);
        }
      
      
      runAway is a function that has a variable named toAlert which is of string type, and then it iterates using for loop and concatenates the runAway string and adds "Lions, Tigers, and Bears, Oh My!!\n" on each iteration. After completion of iteration it alerts the complete string.
      

      您的代码中还有一个右括号。

      你到底没有得到什么?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-21
        • 1970-01-01
        • 1970-01-01
        • 2018-09-09
        • 1970-01-01
        相关资源
        最近更新 更多