简体   繁体   English

'if'语句中的变量声明

[英]Variable declaration inside 'if' statement

My question is potentially embarrassing. 我的问题可能令人尴尬。 I'd consult google if I knew what question to ask specifically, but alas, I'm a code newbie and as such am not familiar with the jargon... 如果我知道要具体问什么问题,我会咨询Google,但是,las,我是代码新手,因此对行话不熟悉...

I'm following a written tutorial @ https://javascript.info . 我正在关注书面教程@ https://javascript.info I'm on the lesson about conditional operators and if statements, having trouble wrapping my head around the behaviour of one of the tasks ( http://javascript.info/ifelse#tasks ), specifically task #4. 我正在上关于条件运算符和if语句的课程,无法将我的头缠在其中一项任务( http://javascript.info/ifelse#tasks )的行为上,尤其是任务4的行为。 "Check the login". “检查登录名”。

Here's the code: 这是代码:

 let userLogin = prompt("Who's there?", ""); if (userLogin == 'Admin') { let pass = prompt("Password?", ""); // ******* if (pass == 'TheMaster') { alert('Welcome!'); } else if (!pass) { alert("Canceled."); } else { alert('I do not know you'); } } else if (!userLogin) { alert("Canceled"); } else { alert("I don't know you") } 

My question revolves around the (****) line. 我的问题围绕(****)线。 The code doesn't work properly if that line isn't nested in the 'if', which threw me way off ( had it as a "global" variable to begin with, had to check the solution because I couldnt find the bug ). 如果该行未嵌套在“ if”中,则代码将无法正常工作,这使我不知所措(首先将其作为“全局”变量,必须检查解决方案,因为我找不到该错误) 。

I ask of you, please, clarify why that is so. 请问我为什么要这样做。 :( :(

edit I didn't realise I was being unspecific. 编辑我没意识到自己是不明确的。 I'm completely new to the forum, won't happen again. 我是该论坛的新手,不会再发生。

The code doesn't work, meaning writing 'Admin' into the first prompt isn't necessary to proceed to the 'pass' prompt. 该代码不起作用,这意味着在进入“通过”提示时无需在第一个提示中写入“管理员”。 This behaviour only happens when I don't nest the "pass" declaration inside 'if'. 仅当我没有将“ pass”声明嵌套在“ if”中时,才会发生此行为。 I think I have found my answer, but wanted to edit as to not further agitate the community. 我已经找到了答案,但想进行编辑以免进一步激怒社区。 :P :P

let userLogin = prompt("Who's there?", "");
let pass = prompt("Password?", ""); // ******* 

if (userLogin == 'Admin') {


  if (pass == 'TheMaster') {
    alert('Welcome!');
  } else if (!pass) {
    alert("Canceled.");
  } else {
    alert('I do not know you');
  }

} else if (!userLogin) {
  alert("Canceled");
} else {
  alert("I don't know you")
}

This is what I mean - hope this clarifies! 这就是我的意思-希望这可以澄清! Sorry again. 再次抱歉。

my guess is that you had the pass either in a separate nested scope, or declared after it was being used. 我的猜测是您在单独的嵌套范围中拥有通行证,或者在使用通行证后对其进行了声明。 let declarations are not hoisted like var declarations. let声明不像var声明那样hoisted So they must be accessed from the same scope, or a more nested scope of that scope, AFTER their declaration. 因此,在声明之后,必须从相同的作用域或该作用域的更多嵌套作用域访问它们。

 let userLogin = prompt("Who's there?", ""); let pass = prompt("Password?", ""); // ******* if (userLogin == 'Admin') { if (pass == 'TheMaster') { alert('Welcome!'); } else if (!pass) { alert("Canceled."); } else { alert('I do not know you'); } } else if (!userLogin) { alert("Canceled"); } else { alert("I don't know you") } 

a link to definition of hoisting if you're curious : https://developer.mozilla.org/en-US/docs/Glossary/Hoisting 好奇的情况下指向吊装定义的链接: https : //developer.mozilla.org/en-US/docs/Glossary/Hoisting

PS - I hope this is just a toy problem... but incase it isn't please do not use this to perform actual authentication. PS-我希望这只是一个玩具问题...但是如果不是, 不要使用它来执行实际的身份验证。 Anything which is in the Javascript of a page is visible to all and your username and password will NOT be secure. 任何在页面的JavaScript是有目共睹的,你的用户名和密码将保。

The only difference I found is that if you remove the line ( *) from the if block the script will ask the user for the **userLogin and the pass , and then do the checking. 我发现的唯一区别是,如果从if块中删除行( *),脚本将要求用户提供** userLoginpass ,然后进行检查。 If you do not, the script will ask for the userLogin and if this is not correct, you will not waste time asking for the pass . 如果您不这样做,脚本将询问userLogin ,如果这不正确,则您不会浪费时间要求通过 As the others say, it would be better to explain what problem you have exactly. 就像其他人说的那样,最好是确切说明您有什么问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM