Good day.
There is a link to some function callBack
, located in an unknown object. Is it possible to get an object or its name, in which our function is located?
Good day.
There is a link to some function callBack
, located in an unknown object. Is it possible to get an object or its name, in which our function is located?
In this case
var callback = object.func;
When a link to a function is passed and possible objects are known, you can:
var objects = [ {id: 1, func: function(){return 3;}}, {id: 2, func: function(){return 2;}}, {id: 3, func: function(){return 1;}} ]; var func = objects[1].func; for(var i=objects.length;i--;){ var obj = objects[i]; for(var prop in obj){ if(obj[prop] == func){ console.log(obj); // {id: 2, func: function(){return 2;}} } } }
UPD:
Another option is to store the link to the object itself in all methods of objects that are intended for use as a callback:
var obj = {possibleCallback: function(){...}}; obj.possibleCallback.root = obj;
Source: https://ru.stackoverflow.com/questions/137453/
All Articles