Javascript function() literal overloading
-
20-08-2019 - |
Question
I was always curious is there any possibility to overload function literal, something like you can do with Function:
var test=Function;
Function=function(arg)
{
alert('test');
return test(arg);
}
var b=Function("alert('a')");
var c=Function("alert('x')");
b();
c();
Of course you can guess that this is nice way of debugging whole project. However any effort I made here goes for nothing.
Question for you experts is:
- Maybe there is something that i don't know, maybe there is possibility to overload this damn constructor? (but probably not).
- If not then - how to do this - if this possible - in any of browser (not just by using javascript - but their extended language - every browser got something like this).
- If not then - how this is done trough addOn like firebug or etc. ??
Solution
You're terminology is off: Function()
is the function constructor, whereas function() {...}
is a function literal.
And no, I don't think there's a portable way to do this, but there might be for old versions of Firefox: If I remember correctly, it once was possible to use with() {...}
to shadow the built-in constructor functions and Firefox would use the new ones even for literals.
This seems to work no longer:
var overload = {
Object : function() {}
};
overload.Object.prototype.foo = 'bar';
with(overload) {
document.writeln(new Object().foo);
document.writeln({}.foo);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow