window.alert("Hello!");
与
相同alert("Hello!");
// Works
Math.sqrt(4);
Math.floor(3.55);
Math.round(2.5);
// Does not work
sqrt(4);
floor(3.55);
round(2.5);
为什么会这样?我什么时候需要键入对象(例如 Math
或 window
),什么时候不需要?
最佳答案
在浏览器中,window
的属性,如 alert
或 document
,是全局的。
即alert
为window.alert
,Math.sqrt
为window.Math.sqrt
.
When do I need to type the object (such as Math or window) and when not?
本质上,如果对象是window
,则您不需要拥有它。对于所有其他情况,您可以这样做。
关于javascript - 调用 alert() 和 sqrt() 的方式有何不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22779627/