So, I have two objects: a and b. Now I want to pass one of the b methods to the object that should store it. Let me call this method b.met:
b.met=function(){
alert(this.txt);
}
Now, I want to call b.met from a. The following code does not work, since a.met is a clone of b.met inside the scope:
a.met=b.met;
a.met();
The only way I found is to store the method name in a string and use it in the eval expression:
a.toCall='b.met';
eval(a.toCall+'();');
Since everyone says you should avoid using eval ... what other options exist?
EDIT - see comments: So, I changed my code:
a:{
processes:[],
spawnProcess:function(type,id,closeFn){
var closeFn=closeFn || 'function(){}';
this.processes.push({type:type,id:id,closeFn:closeFn});
}
at
a:{
processes:[],
spawnProcess:function(type,id,closeFn){
var closeFn=function(){closeFn()} || 'function(){}';
this.processes.push({type:type,id:id,closeFn:function(){closeFn()}});
}
When I execute the following code, I get a too big recursion error:
a.spawnProcess('','',b.met);
a.processes[0].closeFn();