views:

672

answers:

1

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:

  1. Maybe there is something that i don't know, maybe there is possibility to overload this damn constructor? (but probably not).
  2. 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).
  3. If not then - how this is done trough addOn like firebug or etc. ??
+1  A: 

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);
}
Christoph