你如何访问变量而不是重新声明它?或者这样做的另一种选择是什么?

时间:2011-12-05 03:52:13

标签: javascript html

如何解决我在JSBin中遇到的这些错误? JSBin http://jsbin.com/ebizaj/edit#javascript,html 我收到了这些错误。

1.Line 6: var name = prompt("What is your name?", ""); --- 'name' is already defined.
2.Line 12: var PromptAnswer = prompt("So, " + name + " do you play Soccer?", ""); --- 'PromptAnswer' is already defined.
3.Line 35: var PromptAnswer3 = prompt("Well, what sports do you like to play?", ""); --- 'PromptAnswer3' is already defined.

我该怎么办?如果我试图调用变量PromptAnswer3,它将什么都不做。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Javascript Activity</title>
<script type="text/javascript">
function InitAll() 
{
var name = prompt("What is your name?", "");
while (name == null | name == "") {
alert("You didn't answer! :D");
var name = prompt("What is your name?", "");
}
alert("Hello, " + name + " this is a little Javascript Activity!");
var PromptAnswer = prompt("So, " + name + " do you play Soccer?", "");
while (PromptAnswer == null | PromptAnswer == "")  {
alert("You didn't answer, please try again.");
var PromptAnswer = prompt("So, " + name + " do you play Soccer?", "");
}

else if (PromptAnswer.match(/^(yes|yeah)!?$/i))
{
alert("Cool, that's awesome, i play Soccer too!");
var PromptAnswer2 = prompt("Well, even some people play Soccer and don't like it, do you like soccer?", "");
    if (PromptAnswer2.match(/^(yes|yeah)!?$/i)) 
    {
    alert("Yeah, its pretty fun.");
    }
else
{
    alert("Well, that's really too bad, but i guess if you don't like it, you dont like it. :D");
}
}


else {
var PromptAnswer3 = prompt("Well, what sports do you like to play?", "");
    while (PromptAnswer3 == null | PromptAnswer3 == "") 
    {
    alert("You didn't answer");
    var PromptAnswer3 = prompt("Well, what sports do you like to play?", "");
    }
if (PromptAnswer3.match(/^(yes|yeah)!?$/i)) 
    {
    alert("Haha yeah, football is pretty awesome, even though i'm a Javascript Script, my Creator can throw football pretty well. :D");
    }
else
    {
        alert(PromptAnswer3 + " is pretty cool.");
    }
}
}
</script>
</head>
<body onload="InitAll()">
<noscript>
<p>Sorry, you do not have Javascript Enabled for this Activity.</p>
</noscript>
</body>

3 个答案:

答案 0 :(得分:3)

您可以在为其赋值之前声明变量。例如,而不是写

var name = prompt("What is your name?", "");
while (name === null | name === "") {
    alert("You didn't answer! :D");
    var name = prompt("What is your name?", "");
}

你可以写

var name;
name = prompt("What is your name?", "");
while (name === null | name === "") {
    alert("You didn't answer! :D");
    name = prompt("What is your name?", "");
}

答案 1 :(得分:1)

将所有var语句放在函数/脚本的顶部。 var声明变量,您只需要执行一次。

e.g。

var name, PromptAnswer, PromptAnswer2, PromptAnswer3;
...
name = ...;

答案 2 :(得分:1)

在变量名称之前使用var时,您就是在声明它。您只能在within a scope后声明一个给定变量,并且在三条错误消息中的每一条中,您不止一次var [variable name]

您可以稍后重复使用变量,而不必每次都将var放在它们前面。因此,在您的脚本的第6行,只需删除var - 它是重复的,而不是必需的。

删除每条错误消息中提到的每一行上的重复var,然后就完成了。