Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
为什么javascript允许在本地代码中创建全局变量? 一个例子
function f() { x=10; } function g() { print(x); } f(x); g(x);
当您不使用变量开头时,var它们会自动在全局范围内。
var
为什么javascript允许在本地代码中创建全局变量?
因为它不是一种完美的语言。
使用var关键字来限制变量的范围。
我认为您需要var在变量声明之前指定以使其在范围内。