user1018074
user1018074

Reputation: 99

Binding closure variables in Javascript

I'm wondering how I can bind variables to a closure function. Let me use this code:

Please see the code here:

function makeCall(callback) {
var fs = callback.toString();
var stored_callback = eval('(' + fs + ')').bind({ });
stored_callback();
}

function foo(id) {
makeCall(function() {
    console.log(id);
});
}
foo('bar');

After the call of eval, the called function cannot reach the id as it should as a closure.

My question is that before calling the toString, can I somehow retrieve the 'context' of a closure to be stored and retrieved and bind to the call?

Upvotes: 3

Views: 581

Answers (1)

user1018074
user1018074

Reputation: 99

Based on Felix King's answer, it cannot be done. :( Too bad. I just wanted to store calls and executed later for some kind of persistence level. This way an interaction can be made even if the computer restarts or anything... It seems the only way is to not use the embedder parameters from the 'closure', huh? Thanks very much anyway!

Upvotes: 1

Related Questions