JavaScript hint()命令 [英] JavaScript prompt() command
问题描述
我刚刚了解了hint()命令;我知道,hint()命令以字符串形式返回用户输入.我搞砸了下面的程序,并输入了每个"Dead" Ohlin 作为男性名字.为什么这项工作没有引起任何问题?每死"的Ohlin ..."应该引起了问题.解释器是否通过在引号之前放置转义字符来自动解决此问题?
I just learned about the prompt() command; I know that the prompt() command returns user input in the form of a string. I was messing with the program below, and I typed in Per "Dead" Ohlin for the male name. Why did this work and not cause any problems? "Per "Dead" Ohlin..." should have caused a problem. Does the interpreter automatically fix this by putting an escape character before the quotation marks?
let nameOfTheKiller = prompt("Type in a male name.");
let nameOfTheVictim = prompt("Type in a female name.");
let nameOfDrug = prompt("Type in the name of a drug.");
let nameOfAlchoholicBeverage = prompt("Type in the name of an alchoholic beverage.");
let story = nameOfTheKiller
story += " went to a diner, met "
story += nameOfTheVictim + ", and asked her to hangout."
story += " She said yes, so " + nameOfTheKiller + " took her home. As soon as they arrived to "
story += nameOfTheKiller + " relax-location, " + nameOfTheKiller
story += " pulled out " + nameOfDrug + " and " + nameOfAlchoholicBeverage + ". "
story += nameOfTheKiller + " and " + nameOfTheVictim
story += " started using the party favors and got really high and drunk. The party favors gave "
story += nameOfTheKiller + " auditory halucinations that comanded him to kill "
story += nameOfTheVictim + ", so he did." ;
alert("We are done asking you questions. We are generating a story for you. The story will be finished, shortly.");
document.write(story) ;
推荐答案
提示
不是 eval
-传递给它的任何内容都将解释为字符串.输入
prompt
is not eval
- whatever you pass to it will be interpreted as a string. Typing in
Per "Dead" Ohlin
此行运行时
let nameOfTheKiller = prompt("Type in a male name.");
就像在做
let nameOfTheKiller = `Per "Dead" Ohlin`;
您在输入的字符串中包含的任何字符也恰好也是Javascript中的有效字符串定界符,将被解释为这些文字字符(("
,'
,反引号),而不是用作分隔符.
Any characters you include in the string you enter which happen to also be valid string delimiters in Javascript will be interpreted as those literal characters ("
, '
, backtick), rather than as delimiters.
这篇关于JavaScript hint()命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!