启用 "use strict" 后,为什么我可以在使用 prompt() 时分配一个不带 let 的变量

With "use strict" enabled, why can I assign a variable without let while using prompt()

我在脚本的顶部写了 "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.