document is part of browser api but still what if I want to override it though is there a way to force so as this triggers error
https://jsfiddle.net/4yuf5h2o/
var document = {
f: function() {
alert('o')
}
}
document.f();
Not on the top level. In Chrome, for example, window.document is a nonconfigurable getter that returns the document:
console.log(Object.getOwnPropertyDescriptor(window, 'document'));
Since it's non-configurable, it can't be overwritten with anything else.
And you can't declare a new top-level identifier named document either, since it already exists. Use strict mode to make the error explicit:
'use strict';
const document = 'foo';
But you can declare a new variable named document inside an IIFE (so it's not on the top level).
(() => {
var document = {
f: function() {
alert('o')
}
}
document.f();
})();