How dangerous is modifying a function directly?
<p>To get around what has proven to be a scope limitation to me (as answered <a href="http://stackoverflow.com/questions/11388429/is-it-possible-for-a-function-called-from-within-an-object-to-have-access-to-tha/11388441#11388441">here</a>), I've written a piece of code which inserts a line in an anonymous function so that whoever writes the function doesn't have to do it themselves. It's a bit hacky (actually, it feels quite a lot hacky), and I really don't know what I'm doing, so I'd appreciate an expert eye to catch any errors I may have missed or point out any dangers I'm unaware of. Here's the code:</p>

<pre><code>function myObj(testFunc) {
    this.testFunc = testFunc;


    this.Foo = function Foo(test) {
        this.test = test;

        this.saySomething = function(text) {
            alert(text);
        };
    };

    var Foo = this.Foo;
    var funcSep = this.testFunc.toString().split("{");
    funcSep.splice(0, 1);
    funcSep = funcSep.join("{");
    var compFunc =  "    var Foo = this.Foo;" + funcSep;
    compFunc = compFunc.split("}");
    compFunc.splice(compFunc.length - 1, 1);
    compFunc.join("}");
    var otherTestFunc = new Function(compFunc);
    otherTestFunc.apply(this);
}

var test = new myObj(function() {
    var test = new Foo();
    test.saySomething("Hello world");
});
</code></pre>

<p>The function above evaluates as expected, and I don't need to force whoever writes the anonymous function to obtain access to <code>Foo</code> by using <code>this.Foo</code>. This approach feels iffy, though. Is what I'm doing acceptable, and if not, are there any ways to circumvent it?</p>

<p>Also, the only reason I didn't include this in my original question is that seems like something of a departure from the original context of the question.</p>
