为什么打印这个变量显示的是提示,而不是错误信息?

Why does printing this variable show a prompt, instead of an error message?

请问为什么 console.log(theMessage) 显示弹出提示,而不是:"You typed" + textTypedIntoPrompt

或者甚至一些与之相关的错误消息?

编程新手,只是想了解为什么这个程序会这样工作。

谢谢。

var textTypedIntoPrompt = prompt ("Type some text");

var theMessage = "You typed" + textTypedIntoPrompt;

console.log(theMessage);

这不是 console.log which open the popup prompt, it's the prompt 函数

然后在控制台中完成日志,如下面的代码片段所示

var textTypedIntoPrompt = prompt("Type some text");
var theMessage = "You typed : " + textTypedIntoPrompt;
console.log(theMessage);

这里有详细的解释

var textTypedIntoPrompt =                              
// create a variable named textTypedIntoPrompt
                          prompt("Type some text");    
// open the popup prompt
// the popup prompt freeze the execution until the prompt was confirmed or dismissed
// once the prompt is confirmed/dismissed the function returns
// either what the user wrote (if confirmed) or null (if dismissed)

// when you're here textTypedIntoPrompt contains the input
// and your browser already forgot it came from a prompt

var theMessage =                                       
// then you create a variable named theMessage
                 "You typed : " + textTypedIntoPrompt; 
// and you assign to it the string 'you typed : ' followed by the value of textTypedIntoPrompt
// (which is the return value of prompt) 

console.log(theMessage);
// finally you print this string into the console 
// use ctrl+shift+k to open the console in most browsers

最后一个片段证明 console.log 没有打开提示

console.log("didn't open a prompt :)")

虽然这个证明 prompt 确实打开了一个提示(正如它的名字告诉我们的那样)

prompt("I opened a prompt", "you can also set a default value")
// the return value is not saved into a variable so it is lost