How to Gain Access to the Closure of a Function

Is it possible to gain access to the closure of a function?

That's (one of) the purpose(s) of a closure - to keep information private. Since the function already has been executed its scope variables are no longer available from outside (and have never been) - only the functions executed in it's scope (still) have access.

However you could give access via getters/setters.

You might want to take a look into Stuart Langridge's talk about closures. Very recommendable are also Douglas Crockfords Explanations. You can do lots of fancy stuff with closures;)

Edit:
You have several options to examine the closure: Watch the object in the webdeveloper console or (as I do it often) return a debug-function which dumps out all the private variables to the console.

Javascript - Access variable in function closure from outside

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.

javascript: access function in closure

Variables and functions declared in the current lexical scope cannot be accessed by name using [] syntax - only property keys (per your second example) can be looked up dynamically based on the contents of another variable.

The only way around this is to resort to eval, which is almost never a good idea.

An exception applies for variables and functions declared in the global scope - those are actually properties of window.

Accessing variables trapped by closure

Variables within a closure aren't directly accessible from the outside by any means. However, closures within that closure that have the variable in scope can access them, and if you make those closures accessible from the outside, it's almost as good.

Here's an example:

var A = function(b) {
var c = b + 100;
this.access_c = function(value) {
// Function sets c if value is provided, but only returns c if no value
// is provided
if(arguments.length > 0)
c = value;
return c;
};
this.twain = function() {
return 2 * c;
};
};
var a_inst = new A(123);
var my_c = a_inst.access_c();
// my_c now contains 223
var my_2c = a_inst.twain();
// my_2c contains 446
a_inst.access_c(5);
// c in closure is now equal to 5
var newer_2c = a_inst.twain();
// newer_2c contains 10

Hopefully that's slightly useful to you...

JS: When making a closure, how does the inner function access the outer function argument if I'm not storing as a variable?

In example 2 a argument x is passed to the outer function. Inside the outer function every entity has access to x as it's scope is covered in the full code block of that function. When an inner function tries to access that x it can do it easily as it has access to that scope because it itself is present in that scope.
It's just like when you declare a global variable and try to access it inside a function. The function has access to all the global variables.

    var count=0;    function a()    {    console.log(count++)    }        a();

How do I access/modify variables from a function's closure?

A function's enclosed variables are stored as a tuple in the __closure__ attribute. The variables are stored as a cell type, which seems to just be a mutable container for the variable itself. You can access the variable a cell stores as cell.cell_contents. Because cells are mutable, you can change the values of a function's non-local variables by changing the cell contents. Here's an example:

def outer():
x = 1
def inner(y):
nonlocal x
return x + y
return inner

inner = outer()

print(inner(2)) # 3
print(inner.__closure__) # (<cell at 0x7f14356caf78: int object at 0x56487ab30380>,)
print(inner.__closure__[0].cell_contents) # 1

inner.__closure__[0].cell_contents = 10
print(inner(2)) # 12
print(inner.__closure__[0].cell_contents) # 10

EDIT - the above answer applies to Python 3.7+. For other Python versions, you can access the closure the same way, but you can't modify the enclosed variables (here's the Python issue that tracked setting cell values).



Related Topics



Leave a reply



Submit