Reputation: 290
In the website the following lines are given:
Your global variables (or functions) can overwrite window variables (or functions). Any function, including the window object, can overwrite your global variables and functions.
I am unable to understand what this says.
Upvotes: 2
Views: 53
Reputation: 8523
The browser puts certain "global" (i.e. accessible anywhere in any file) functions and variables on the window
object. One such example is the function window.setTimeout
, which executes its argument after a given delay.
You can also access these global window.*
variables without the window prefix at all, i.e.
window.setTimeout === setTimeout
That means if you assign to a global variable with a conflicting name, you can 'override' the browser defaults -
window.setTimeout === setTimeout
setTimeout = 'myString'
window.setTimeout === 'myString'
That's why it's generally best practice not to create variables in the global (window
) scope.
Upvotes: 2
Reputation: 62566
Above that paragraph you can find this:
Global Variables in HTML With JavaScript, the global scope is the complete JavaScript environment.
In HTML, the global scope is the window object. All global variables belong to the window object.
Combined with the quote in your question - it means that because the global scope is actually the window
object - it's possible to override global functions/variables.
Here is an example:
console.log(window.Math.PI);
Math = {
PI: 5
}
console.log(Math.PI);
Upvotes: 2