I'll probably best explain this through code. I got something like this:
var object1 = function(){
//do something
}
var object2 = function(){
//do something else
}
var objects = {
'o1' : object1,
'o2' : object2
};
var actions = [];
function addAction( actionName ){
var object = objects[actionName];
actions.push( function(){ new object(); } );
}
So this code, saves a sequence of runtime-determined actions based on user input that are saved in an array.
addAction( "o1" );
addAction( "o2" );
If I want to replay that sequence I just do:
for( i in actions ){
actions[i]();
}
and this will create two anonymous objects of type object1 and object2.
Now, I need somehow to serialize the actions[] array but I need each of the functions within it to retain it's scope. If I just cast the functions to strings I get:
"function(){ new object(); }"
and if I eval this string, then 'object' would be undefined. How would you do this ?