Whoa! That's super hacky, but totally awesome.
I knew that Angular does some funky stuff with reading a function's toString value, to do dependency injection, but I had no idea that you could manipulate the function's string representation directly like that.
Let me walk through to make sure that I'm understanding, correctly, though.
- We get the function's string value (which is the same as the text that declared it).
- We manipulate that text, replacing "Foo" with "New Foo".
- We eval the resulting string, which declares a new function "foo" with a different definition than the previous function "foo".
Doesn't that mean that in fact, the variables in the original "foo" were private? And we've just got an entirely new "foo" with a different set a variables?
Regardless of the details, that's crazy and cool. :-)
Joshua Clanton