javascript - Should one write window.X when referring to a built-in global property X in the (desktop) browser? -
Therefore, (desktop) browsers have dozens of built-in global properties, for example:
-
documentundefinedparseIntJSONLocationWarningsetTimout- etc.
While referring to those qualities, they should clearly note them as global property by prefixing them with their properties
window. ? Therefore, for example: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:
-
Yes, you always write
window.XNo, you do not have to write
window.X . JustX is OK.For some properties it depends on the property, use
window.X , for some other properties,X (If this is your answer, expand it.)So, what is it?
I leave a few exceptions and go to 3: No
window .In the browser, the
window refers to the global scopewindow. As unnecessary inwindow.prompt () , you can emphasize it to use thatprompt () awindow is a method of object.I will never use anything like
window math orwindow.NaN because these properties are global objects that havewindows < There is nothing with the code / object which is a global object in the browser, also see.If you have another variable in the existing (local) area of the name
prompt , you will need thewindow. Initially as prefix:(function () {var prompt = "Give me your name!" Var name = window.prompt (prompt, "your name") ;}));For the setting of global variables, you should add the
window. To satisfy the equipment as a prefix (Otherwise, you might have forgottenvar and accidentally leak a variable in the global scope):(function () {// "wrong" somevar = 1; // You probably want to set a local variable, so should use: var somevar = 1; // remove confusion , You really want to set global variables: window.somevar = 1;}) ();Generally, except the
window By considering the next example, readability improvements: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 ("average"). Value = window.Math.floor ((numA + NumB) / 2);}, 1000);
Comments
Post a Comment