Javascript 在if语句中存储变量值

Javascript 在if语句中存储变量值,javascript,variables,if-statement,storage,Javascript,Variables,If Statement,Storage,如果条件为: if (currentQuestionIndex == 2) { var xvalue = currentscore; } 第二个if条件:(注意:当前分数每currentQuestionIndex增加一次) 我想在第一个if条件下访问xvalue的值,并在第二个if条件下引用它。有没有办法存储变量的值并在以后引用它而不考虑if条件您可以在if语句之外设置xvalue变量 var xvalue=0 或者简单地说: var-xvalue 这样,任何其他函数都可以访问变量,即

如果条件为:

if (currentQuestionIndex == 2) {
    var xvalue = currentscore;
}
第二个if条件:(注意:当前分数每
currentQuestionIndex
增加一次)


我想在第一个if条件下访问
xvalue
的值,并在第二个if条件下引用它。有没有办法存储变量的值并在以后引用它而不考虑if条件

您可以在if语句之外设置
xvalue
变量

var xvalue=0

或者简单地说:

var-xvalue


这样,任何其他函数都可以访问变量,即使您没有给它赋值。

您可以在if语句之外设置
xvalue
变量

var xvalue=0

或者简单地说:

var-xvalue


这样,任何其他函数都可以访问变量,即使您没有给它赋值。

只需在第一个
之外声明
xvalue
,如果
如下所示:

var xvalue; // decalare it here so it can be accessed by both if statements (probably you'll have to initialize it with something)
if (currentQuestionIndex == 2) {
    xvalue = currentscore; // don't redeclare it here so you won't shadow the above one
}

阅读有关范围变量生存期阴影的更多信息

只需在第一个
之外声明
xvalue
,如果
如下所示:

var xvalue; // decalare it here so it can be accessed by both if statements (probably you'll have to initialize it with something)
if (currentQuestionIndex == 2) {
    xvalue = currentscore; // don't redeclare it here so you won't shadow the above one
}

阅读有关范围变量生存期阴影的更多信息

只需在
if
语句之外定义变量
xvalue

///仅用于测试
var currentQuestionIndex=2
var currentscore=2
///
var xvalue=0;
如果(currentQuestionIndex==2){
xvalue=currentscore;
}
///只是为了测试
currentQuestionIndex=5
当前分数=4
console.log('xvalue:'+xvalue)
///
如果(currentQuestionIndex==5&¤tscore==xvalue){
console.log('感谢您的参与')
///只是为了测试
console.log('xvalue:'+xvalue)
///

}
只需在
if
语句之外定义变量
xvalue

///仅用于测试
var currentQuestionIndex=2
var currentscore=2
///
var xvalue=0;
如果(currentQuestionIndex==2){
xvalue=currentscore;
}
///只是为了测试
currentQuestionIndex=5
当前分数=4
console.log('xvalue:'+xvalue)
///
如果(currentQuestionIndex==5&¤tscore==xvalue){
console.log('感谢您的参与')
///只是为了测试
console.log('xvalue:'+xvalue)
///

}
实际上,您可以访问
var
内部
if
语句中声明的变量。如果
块(或任何块)没有创建新的作用域,则为
:在JavaScript中,只有函数才能创建新的作用域

这是有效的(示例来自):

这是因为JavaScript解释器在执行函数之前扫描函数,并跟踪所有变量声明(如将其移到顶部)

因此,上述代码相当于(解释器透视图):

引述自:

ECMAScript 2015之前的JavaScript没有语句范围; 相反,在块中声明的变量是函数的局部变量 块所在的(或全局范围)

如前所述,随着ECMAScript 2015的引入,这种行为发生了变化

因此,以您的代码为例,这将运行良好:

/* arbitrary values */
var currentQuestionIndex = 2;
var currentscore = 10;

if (currentQuestionIndex == 2) {
    var xvalue = currentscore; //declared inside if block
}


if (currentQuestionIndex == 2 && xvalue == 10) { //I can read here
    console.log('value of xvalue: ' + xvalue); //can read here too: will print 10
    console.log('thanks for your participation')
}

不管怎样,仅仅因为你能,并不意味着你应该。就可读性和代码组织而言,推荐的做法是在作用域的开头声明变量(全局,如果在任何函数之外,或局部,如果在函数内部)。

实际上,您可以访问使用
var
inside
if
语句声明的变量。如果
块(或任何块)没有创建新的作用域,则为
:在JavaScript中,只有函数才能创建新的作用域

这是有效的(示例来自):

这是因为JavaScript解释器在执行函数之前扫描函数,并跟踪所有变量声明(如将其移到顶部)

因此,上述代码相当于(解释器透视图):

引述自:

ECMAScript 2015之前的JavaScript没有语句范围; 相反,在块中声明的变量是函数的局部变量 块所在的(或全局范围)

如前所述,随着ECMAScript 2015的引入,这种行为发生了变化

因此,以您的代码为例,这将运行良好:

/* arbitrary values */
var currentQuestionIndex = 2;
var currentscore = 10;

if (currentQuestionIndex == 2) {
    var xvalue = currentscore; //declared inside if block
}


if (currentQuestionIndex == 2 && xvalue == 10) { //I can read here
    console.log('value of xvalue: ' + xvalue); //can read here too: will print 10
    console.log('thanks for your participation')
}

不管怎样,仅仅因为你能,并不意味着你应该。就可读性和代码组织而言,推荐的做法是在作用域的开头声明变量(全局,如果在任何函数之外,或局部,如果在函数内部)。

我尝试过这一点,但这里currentscore会随着currentQuestion索引的增加而增加,并且不是常数。所以对于currentQuestionindex,除了两个,它返回zero@eggsy我刚才回答了你的问题:是否有任何方法来存储变量的值,并在后面引用它,而不考虑if条件。在第一条件下考虑xValue= 2,在第二if条件中引用xValue= 0(全局变量),我不是这样的人expecting@eggsy这里我添加了代码片段和一些测试变量。它工作正常:xvalue=2感谢您的努力。目前的分数不是固定的。在这种情况下,它被分配给两个。对于当前指数==2,概率更高的当前分数为2,对于当前问题==5,概率更高的当前分数可能为4。如果用户给出正确答案(此处未指定),则当前分数将增加。我的问题是当前分数一旦达到currentQuestionIndex==5就会更新,所以x值变为4而不是2。我确实尝试过这个,但这里currentscore随着currentQuestionIndex的增加而增加,并且不是常数。所以对于currentQuestionindex,除了两个,它返回zero@eggsy我只是
if (true) {
  let x = 5;
}
console.log(x);  //x is not defined
/* arbitrary values */
var currentQuestionIndex = 2;
var currentscore = 10;

if (currentQuestionIndex == 2) {
    var xvalue = currentscore; //declared inside if block
}


if (currentQuestionIndex == 2 && xvalue == 10) { //I can read here
    console.log('value of xvalue: ' + xvalue); //can read here too: will print 10
    console.log('thanks for your participation')
}