3

I am running into an issue with closures and want to confirm a suspicion. I am trying to give a function access to a scope outside of where it was originally defined. For example the following code block works as I would expect.

(function(){
 function Outer () {
 var test = "closure access";
 return function Inner () {
 alert(test);
 };
 };
 // call outer then inner
 Outer()();
}());

When run the string "closure access" is alerted.

However if the inner function is defined outside of the "Outer" scope and then returned from the "Outer" function it no longer has access to the local "test" variable. For example:

(function(){
 function Inner () {
 alert(test);
 };
 function Outer () {
 var test = "closure access";
 return Inner;
 };
 // call outer then inner
 Outer()();
}());

This behavior makes sense to me as the "Outer" function is actually looking beyond it's normal scope to find the "Inner" function defined above it, and the Inner function is not defined within the scope of the "Outer"'s local var "test". My question is, is there a way to define a function somewhere (elsewhere) but dynamically return it from another function while still preserving the variables of its closure function?

Please let me know if I must explain better.

==== Edit ====

Ok, so defining the function creates the scope and not using the function. Does the following code snippet not define a function?

(function(){
 function Outer () {
 var test = "closure access";
 return new Function("alert(test);");
 };
 // call outer then inner
 Outer()();
}());

If the new Function is defining a function at that point in memory should it not have access to the test variable?

asked Jul 8, 2014 at 20:02
2
  • 3
    no, function scope is created by defining functions, not by using them. Commented Jul 8, 2014 at 20:08
  • That would essentially give us private properties and methods. Alas, we have to resolve to put everything in the constructor. Commented Jul 8, 2014 at 20:12

2 Answers 2

0

My question is, is there a way to define a function somewhere (elsewhere) but dynamically return it from another function while still preserving the variables of its closure function?

No. You need to define a function there to access variables from that scope.

You could however dynamically use other functions in that one, so that you can somehow simulate this behaviour; nonetheless you always need to create a new function.

Does the following code snippet not define a function?

return new Function("alert(test);");

No. The Function constructor creates a function without a scope chain, i.e. as if it was declared in the global scope. You'd need to use eval.

answered Jul 8, 2014 at 21:03
Sign up to request clarification or add additional context in comments.

Comments

0

If all functions are defined under same object then u can use this

(function(){
 function Inner () {
 alert(this.test);
 };
 function Outer () {
 this.test = "closure access";
 return Inner;
 };
 // call outer then inner
 Outer()();
}());

but if functions are defined under different object then u can use either prototype.call(this) or prototype.apply(this) to pass the reference and in that way u could have access to other object property.

answered Jul 8, 2014 at 21:46

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.