coding style - Why 'window.onload' but simply 'document' instead of 'window.document' in conventional JavaScript code? -


in javascript come across, see programmers write window.onload instead of writing onload.

but when comes accessing document object, write document instead of window.document.

is there specific reason inconsistency in coding convention?

the document available in scope, unless it's overwritten, doesn't happen.

that means 1 can do

var obj = {     key : function() {         return document.getelementbyid('test'); // still document     }  }    console.log( obj.key() );
<div id="test"></div>

this beacuse document in global special variable available, window

on other hand, elements have onload function, of time 1 isn't dealing window.onload if writing onload.

not that, being property of object, depends on this being window, in global scope.
create other scope, , onload other window.onload

var obj = {     key : function() {         onload = function() {     // not window.onload             console.log('test');         };     }  }    console.log( obj.key() );


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -