I have a function declaration, e.g.
function a(){alert("Hello");}
and I'd like to have a script to be run before the above script so that above function declaration have no effect, i.e. it should not throw errors and a(); should not open an alert window.
If it was a variable assignment like
var a = function (){ alert("Hello");};
I could do
Object.defineProperty(window, 'a', {value: function(){}});
so that a is not writable and assignments that happens after that has no effect.
However, the same thing seems to be impossible with function declarations. No matter how I set writable/configurable, the new function declaration is applied or it throws errors, such as
Uncaught SyntaxError: Identifier 'a' has already been declared (Chrome)
TypeError: cannot declare global binding `a': property must be configurable or both writable and enumerable (Firefox)
ES6 Proxy seemed promising, because it has defineProperty handler, but it seems to be impossible to 'proxy' the window object.
Now I have a feeling that because of how function declaration is implemented either by browsers or by ecmascript specifications, it is impossible to achieve this. Is there any way to achieve this?
Alternatively, if there is a way to get notified when a function a is declared, I could do window.a = function (){} then. Is there such a way?