How to find out which element stores a data object

If I pass the data object to a function, as in:

$("#someobject").data({ "prp1":"x", "dosomething":function(){ callthisfunction(this); //<---- HERE the data ref is sent to a function } }); ... function callthisfunction(in_data) { //how is the data element? var theElementHoldingTheDataIs = in_data.????; //<--- how can I get $("#someobject") } 

My questions: is there a way from the data to tell which object it depends on or belongs to?

+4
source share
1 answer

You can use closure:

 var obj = $("#someobject"); obj.data({ "prp1": "x", "dosomething": (function(scope) { return function() { callthisfunction(scope); //<---- HERE the data ref is sent to a function } })(obj) }); 

Example

<h / "> Or if you just want to send a data object:

 var obj = $("#someobject"); obj.data({ "prp1": "x", "dosomething": (function(scope) { return function() { callthisfunction(scope.data()); } })(obj) }); 
+2
source

Source: https://habr.com/ru/post/1388145/


All Articles