我写"use strict";
在我的剧本的顶部。
我不能写num = 5;
,因为我得到了ReferenceError: Can't find variable: num
。
为了解决这个问题,我可以写let num = 5;
.
使用这种逻辑,为什么我可以写作name = prompt("What is your name?");
?
我不应该写let name = prompt("What is your name?")
吗?
我写"use strict";
在我的剧本的顶部。
我不能写num = 5;
,因为我得到了ReferenceError: Can't find variable: num
。
为了解决这个问题,我可以写let num = 5;
.
使用这种逻辑,为什么我可以写作name = prompt("What is your name?");
?
我不应该写let name = prompt("What is your name?")
吗?
假设您在浏览器中运行它,您会因为该window.name
属性而看到这种行为。您的第二个示例是将返回的值存储prompt
在此属性中。
如果 window 具有内置num
属性,您的第一个示例也可以。
当然,在实际代码中,您希望创建一个变量来存储prompt
值而不是破坏window.name
。