I would go for 3: no window
except for a few exceptions.
In browsers, window
refers to the global scope. window.
as in window.prompt()
is redundant. You could use it to emphasize that prompt()
is a method of the window
object.
I would never use something like window.Math
or window.NaN
because these properties are global objects that has nothing to do with the window
object which is incidentally the global object in browsers. See also Global Properties and Functions Defined in ECMAScript.
If you have another variable in the current (local) scope named prompt
, you would need the window.
prefix as well to get the prompt dialog as in:
(function() {
var prompt = "Give me your name!";
var name = window.prompt(prompt, "your name");
})();
For setting global variables, you should add the window.
prefix as well to satisfy tools like jslint. (otherwise, it would look like a you have forgotten the var
keyword and thereby accidentally leaks a variable in the global scope):
(function() {
// "WRONG"
somevar = 1;
// You probably want to set a local variable, so should use:
var somevar = 1;
// take away the confusion, you really wanted to set a global variable:
window.somevar = 1;
})();
Generally, omitting window.
improves readability, considering the next example:
window.setInterval(function() {
var numA = window.parseInt(window.document.getElementById("numA").value, 10);
var numB = window.parseInt(window.document.getElementById("numB").value, 10);
window.document.getElementById("avg").value = window.Math.floor((numA + numB) / 2);
}, 1000);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…