Reputation: 26939
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)?
Upvotes: 1
Views: 156
Reputation: 24614
SpiderMonkey and JScript implements a non-standard "caller" property on Function-objects. I don't know the extent of the support in other browsers.
I belive it should be accessible like this:
Function.prototype.myMethod = function() {
var fn = this;
var owner = this.caller;
/** ... */
};
but haven't tested it. As I said, it's not part of ECMA standard.
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/Caller
Upvotes: 0
Reputation: 7426
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 ) {
// ...
Upvotes: 1