Search code examples
javascriptfunctionthisprototype-programming

Accessing the two 'this' variables in Function.prototype


I'd like to extend Function.prototype with a custom method:

Function.prototype.myMethod = function() {
     var fn = this;
     var owner = ???;
     /** ... */
};

this in that scope refers to the original function. But how do I access the this that refers to the object that "owns" the function (or whatever the outer this is at the time that fn is called)?


Solution

  • You would need to pass it in as a parameter to the function call... not sure if this is possible in your specific usage.

    myMethod(this);
    
    function myMethod( parent ) {
        // ...