When creating a global object in a <script> tag in HTML, it's my understanding that the global object is put into the DOM object window. But to reference the global object, you don't need to explicitly use window. as a prefix.
<script>
myGlobalObject = {};
myGlobalObject.myProperty = "testing";
window.myGlobalObject.myProperty2 = "testing2";
//get value from global
var retrievedValue = myGlobalObject.myProperty;
//get another value
var anotherVal = window.myGlobalObject.myProperty2;
</script>
I think that I also read somewhere that the browser will first look through the window object, and it will match anything with the same name? So, I guess using window. becomes immaterial? Just looking for a definitive clarification. Obviously, it's more typing to add window. to everything. Is there any downside to NOT prefixing a global object with window.?
Aucun commentaire:
Enregistrer un commentaire