在javascript的文字函数中改变变量的值

change the value of a variable within a literal function in javascript

本文关键字:改变 变量 函数 javascript 文字      更新时间:2023-09-26

我想知道如何得到这个结果:

function() {
   var myVar = "";
   function () {
      myVar += "This is ";
   }
   myVar = "my string";
   alert(myVar); //would like it to display: This is my string
   // only displays: my string
}

你a .必须调用一个函数来执行它B.必须重新组织你的字符串位

function() {
   var myVar = "";
   function update() {
      myVar = "This is " + myVar;
   }
   myVar = "my string";
   update();
   alert(myVar); //would like it to display: This is my string
   // only displays: my string
}

你必须调用这个函数。

var myVar = "";
function someFunction() { // Give it a name
   myVar += "This is ";
}
myVar = "my string";
someFunction(); // and call it
alert(myVar);