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
Post a Comment