Using a window object

I have seen many people using window.a variable when calling it. But aren't all the variables of one window actually in window?
Example:

window.onorientationchange = function() {
  var orientation = window.orientation; // <-- WHY?
  switch(orientation) {
    /* ... */
  }
}

But the same people use alert(), documentetc. So why?

+3
source share
2 answers

At certain points in time, you want to set only certain functions / variables for explicit properties window.

(function() {
   var jQuery = function(selector) { alert( selector ) }
})();

jQuery // not defined

If we change it as:

(function() {
   var jQuery = function(selector) { alert( selector ) }
   window.jQuery = jQuery;
})();

then we “explicitly expose” it from the private namespace.

, window. , alert, , , .

window. , alert, . , , alert, ...

(function() {
   function alert() {}
   alert('lol'); // does nothing
   window.alert('lol') // does something
})();

alert , , . window.alert, .

, window., . , , document alert , , .

+2

javascript- alert(), document() .. , , .

0

Source: https://habr.com/ru/post/1782467/


All Articles