Are jQuery events automatically unrelated when destroying what they are associated with?

If I have the following code in two functions of an object:

add: function()
{
    // create trip.
    var trip = new Trip();

    // add the trip using its id.
    this.trips[trip.id] = trip;
},

remove: function(tripId)
{
    // remove trip.
    delete this.trips[tripId];
}

NOTE. The constructor of the Trip object binds a bunch of custom jQuery event handlers.

Will the event handlers associated with the Trip object be automatically destroyed / cleared when the Trip object is deleted?

Will the event be the same for the dom node if it has been deleted and have event handlers contacted it?

I also read that objects are not cleared by the garbage collector until all references to them no longer exist, therefore the event handlers associated with the object themselves are considered links and do not allow you to clear the object, even if I no longer refer to him?

+3
3

, jQuery , delete. , . ( jQuery 1.4.2)

jsfiddle:

// 1. a regular JS object
var root = {};
// Since we can't delete anything created with var (except on Chrome),
// we use an object property here. An array value works just as well,
// which is already the case in your example.
root.o = {};

// 2. at this point, jQuery creates an internal property
// jQuery<UNIQ_ID>, for example jQuery1277242840125 inside object o
$(root.o).bind("myEvent", function() { alert("here"); });

// 3. get the *internal* index jQuery assigned this object:
// there only 1 property, so we just enumerate and fetch it.
var internalIndex;
for(var prop in root.o) {
    internalIndex = root.o[prop];
}

// 4. delete the object
delete root.o;

// 5. query jQuery internal cache with the internal index from step 3
console.log(jQuery.cache[internalIndex].events);
​

5 , ex-o, "myEvent" , . , , ( ):

β–Ύ Object
  β–Ύ myEvent: Array (1)
    β–Ύ 0: Object
      β–Έ handler: function () { alert("here"); }
        namespace: ""
        type: "myEvent"
      length: 1

, , . , , , - jQuery, .

, , JavaScript, . , jQuery , DOM node :

Uncaught TypeError: Object #<an Object> has no method 'removeEventListener'

, , . , :

$(object).remove()
$(object).unbind(..)

a >

Trip removeData .

$(object).removeData();

, jQuery, , , .

+5

, , , , .. DOM . , . . .

0

dom node, ?

this.handlerClick = function () { ... };

$(this.testDomNode).bind('click', this.handlerClick);

this.testDomNode.parentNode.removeChild(this.testDomNode);

FireQuery FireFox, dom node ,

It seems you need to explicitly untie the handler before removing the dom node as follows:

$(this.testDomNode).unbind('click', this.handlerClick);

this.testDomNode.parentNode.removeChild(this.testDomNode);
0
source

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


All Articles