1

I was wondering if there was some way to obtain the value of a variable that has been captured by a nested function, from outside of that said function. A little tricky to explain in words so here's what I'm trying to achieve:

function counter(){
    let count = 0;
    return function counterIncrementer(){
      ++count;
    }
}

function someReceiever(counterIncrementer){
    // From here, somehow access the value of count captured by 
    // counterIncrementer.
    // -> Without modifying the counter/returned counterIncrementer function 
    //    before runtime
}
someReceiever(counter())

Thanks !

4
  • 1
    Why not just returning that value? Commented Nov 5, 2018 at 23:29
  • Because I didn't write the 'counter' function and the variable was meant to be hidden. This was just for easy illustrative purposes. Commented Nov 5, 2018 at 23:29
  • 2
    there is no way to do that, the count in that case is available only in its context. and it is private to that as it is not exported in any way shape of form. Commented Nov 5, 2018 at 23:31
  • Ahh damn, that makes things a little more tricky. Commented Nov 5, 2018 at 23:37

2 Answers 2

1

By my best knowledge - it's not possible to do that. The count value is a part of the counter function and is accessible only in this scope or in the scope of functions created inside that function (so basically closures).

I think you might find this thread useful: Accessing variables trapped by closure

Sign up to request clarification or add additional context in comments.

2 Comments

Do you know if it'd be possible to somehow create a new counterIncrementer function with a clone of its original scope or is that beyond the realm of possibility? If that was possible you could return a function with the captured variable.
If you know the scope, then you can use eval to do that: jsbin.com/wekoxokibe/edit?js,console . If you don't know the scope, then you would probably have to manually parse the scope function, which in more complex scenarios might be virtually impossible to do.
0

The only way to do this would be to declare count as a global, or create another function just for accessing count, nested within counter; but given the structure of your code, it doesn't seem like that's a great answer.

function counter(){
    let count = 0;
    return [
        function counterIncrementer(){
            ++count;
        }, 
        function counterGetter() {
            return count;
        }
    ];
}

function someReceiever(counterIncrementerPack){
    let counterIncrementer = counterIncrementerPack[0];
    let counterGetter = counterIncrementerPack[1];

    console.log(counterIncrementer(), counterGetter(), counterGetter(), counterIncrementer(), counterGetter());
}
someReceiever(counter())

Outputs: undefined 1 1 undefined 2

Note: you may also want to make counterIncrementer return ++count, but that wasn't the question shrug.

Comments

Your Answer

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.