JavaScript hint()命令

时间:2019-03-14 23:24:05

标签: javascript ecmascript-6

我刚刚了解了hint()命令;我知道提示()命令以字符串形式返回用户输入。我在弄乱下面的程序,然后输入每个“ Dead” Ohlin 作为男性名字。为什么这项工作没有引起任何问题? “每“死”的Ohlin ...”应该引起了问题。解释器是否通过在引号之前放置转义字符来自动解决此问题?

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) ;

1 个答案:

答案 0 :(得分:6)

prompt不是eval-传递给它的任何内容都将解释为 string 。输入

Per "Dead" Ohlin

此行运行时

let nameOfTheKiller = prompt("Type in a male name.");

就像在做

let nameOfTheKiller = `Per "Dead" Ohlin`;

您输入的字符串中包含的任何字符也恰好也是Javascript中的有效字符串定界符,将被解释为这些文字字符"',反引号),而不是作为分隔符。