Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
375 views
in Technique[技术] by (71.8m points)

javascript - Should one write window.X when referring to a built-in global property X in the (desktop) browser?

So, there are dozens of built-in global properties in the (desktop) browser. For instance:

  • document
  • undefined
  • parseInt
  • JSON
  • location
  • alert
  • setTimout
  • etc.

When referring to those properties, should one explicitly note them as global properties by prefixing their name with window.? So, for instance:

var wrap = window.document.getElementById('wrap');

and

window.setTimeout(loop, 100);

and

var x = window.parseInt(input.value, 10);

I think there are three answers to this question:

  1. Yes, you should always write window.X when referring to global properties.

  2. No, you don't have to write window.X. Just X is fine.

  3. It depends on the property. For some properties, use window.X, for some other properties use X. (If this is your answer, please elaborate.)

So, which is it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...