That is/was possible, but it is restricted. First restriction, not all Javascript engines support the following pattern and second (more dramatic), ES5 strict mode does not support it either.
Some Javascript engines allow you to access the .__parent__
property which is a reference to the parent scope. That should look like
alert('my parent is ' + this.__parent__.name );
where you would have to call new closure()
, or give the function a name and use that name instead of this
.
However, most implementations removed that access for security concerns. As you may noticed, you're able to breach the 'allmighty' closure security by beeing able to access the parent context.
Another way to accomplish it, is to access arguments.callee.caller
, which also is not accessible in ES5 strict mode. Looks like:
function MyFunction() {
closure = function f() {
alert('my parent function name is: ' + arguments.callee.caller.name);
}
closure();
}
MyFunction();