为什么 JavaScript 中变量要在函数之前声明

发布于 2024-10-31 04:31:44 字数 243 浏览 1 评论 0原文

我在网上寻找计算器代码,发现了如下代码。

但我心里有一个问题。为什么程序员要在创建函数之前声明变量?

var getValues= "";

function updateField(val) {
    getValues += val;
    document.calc.putValues.value = getValues;
}

请帮我回答我的问题。

谢谢大家。

I was looking for calculator codes on net and I discovered code like the following.

But i have a question in my mind. Why did the programmer declare the variable before creating the function?

var getValues= "";

function updateField(val) {
    getValues += val;
    document.calc.putValues.value = getValues;
}

Please kindly help me answer my question.

Thank you all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

永不分离 2024-11-07 04:31:44

这样,它就是一个全局变量,可以通过函数调用保留其值。
如果你把它放在函数内部,当函数被调用时它总是0

That way it's a global variable that persists its value through the function calls.
If you put it inside the function it will be always 0 when the function is called

长伴 2024-11-07 04:31:44

他所做的是将变量移出函数的范围。

这将使同一变量可以通过同一范围内的其他方法访问。

请参阅此问题以了解有关变量作用域的更多信息:JavaScript 中变量的作用域是什么?

What he's doing is he's moving the variable out of the scope of the function.

This will make the same variable accessible by other methods in the same scope.

See this question to learn more about variable scope : What is the scope of variables in JavaScript?

丑疤怪 2024-11-07 04:31:44

要知道,变量其实是可以在函数下声明的。但它必须在使用之前声明,即在调用函数之前声明。

我创建了一个测试场景来展示我的意思。

我创建了一个名为 test.html 的文本文件,其中包含以下简单内容:

<script type="text/javascript">
var a = "hello"; // <- the declaration of the variable before the function
function b(){ // <- the actual function
  a += " world";
  alert(a);
}
b(); // <- and here it is called
</script>

如果我在 Firefox4 中加载此文本文件(文件://$path_to_file/test.html),我会收到一个警告框,其中包含消息你好世界

然后我改变了顺序:

<script type="text/javascript">
function b(){ // <- the actual function
  a += " world";
  alert(a);
}
var a = "hello"; // <- the declaration of the variable under the function
b(); // <- and here it is called
</script>

结果是一样的:Hello World
但是当我将声明放在这样的调用下时:

<script type="text/javascript">
function b(){ // <- the actual function
  a += " world";
  alert(a);
}
b(); // <- and here it is called
var a = "hello"; // <- the declaration of the variable under the call
</script>

我得到了不同的结果:undefined world。 JavaScript 认识到它不知道 a 可能是什么,因此将其处理为 undefined

当然,数字之和可能与字符串之和的解释不同,所以我也测试了这一点:

<script type="text/javascript">
function b(){ // <- the actual function
  a += 3;
  alert(a);
}
b(); // <- and here it is called
var a = "hello"; // <- the declaration of the variable under the call
</script>

结果是:NaN 意思是不是数字

这就是JS的懒惰和宽容。您的问题当然也可以解释为关于变量和函数的范围。但对此已经有两个答案。当然,如果还不够的话我也可以在这里编辑详细的解释。

You know, the variable can actually be declared under the function. But it must be declared before it is used, meaning before the function is called.

I created a test scenario to show what I mean.

I created a textfile called test.html with the following simple content:

<script type="text/javascript">
var a = "hello"; // <- the declaration of the variable before the function
function b(){ // <- the actual function
  a += " world";
  alert(a);
}
b(); // <- and here it is called
</script>

If I load this text file in my Firefox4 (with file://$path_to_file/test.html) I get an alert box with the message Hello world.

Then I changed the order:

<script type="text/javascript">
function b(){ // <- the actual function
  a += " world";
  alert(a);
}
var a = "hello"; // <- the declaration of the variable under the function
b(); // <- and here it is called
</script>

The result was the same: Hello World
But when I was putting the declaration under the call like this:

<script type="text/javascript">
function b(){ // <- the actual function
  a += " world";
  alert(a);
}
b(); // <- and here it is called
var a = "hello"; // <- the declaration of the variable under the call
</script>

I got a different result: undefined world. JavaScript recognizes that it doesn't know what a could be and so handles it as undefined.

Of course a sum of numbers could have been interpreted differently from a sum of strings, so I also tested this:

<script type="text/javascript">
function b(){ // <- the actual function
  a += 3;
  alert(a);
}
b(); // <- and here it is called
var a = "hello"; // <- the declaration of the variable under the call
</script>

The result was: NaN meaning Not a Number.

That is all about laziness and tolerance of JS. Your question can of course also be interpreted concerning the scope of variables and functions. But for that there are already 2 answers. Of course, if they are not enough I can also edit a detailed explanation here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文