Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The event will not be deleted as jQuery maintains a central repository of all bound event handlers, and does know if or when you deleted an associated object using <code>delete</code>. Try this little test to confirm. (jQuery 1.4.2 only)</p> <p><a href="http://jsfiddle.net/BUkNF/" rel="nofollow noreferrer">jsfiddle link</a>:</p> <pre><code>// 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&lt;UNIQ_ID&gt;, for example jQuery1277242840125 inside object o $(root.o).bind("myEvent", function() { alert("here"); }); // 3. get the *internal* index jQuery assigned this object: // there's 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); ​ </code></pre> <p>Step 5 should log an array of all event types that were associated with the ex-o object, including "myEvent", and it's associated handler, so no the bound events handlers will not delete automatically. Here's what I see get logged (stripped out irrelevant properties):</p> <pre><code>▾ Object ▾ myEvent: Array (1) ▾ 0: Object ▸ handler: function () { alert("here"); } namespace: "" type: "myEvent" length: 1 </code></pre> <p>The object deletion, however, is not affected, and that will be deleted as expected. However, it is a hole in the wall kind of a situation since there is associated data somewhere in jQuery's cache that will remain there.</p> <p>It seems that although you can bind events to plain JavaScript objects, you cannot unbind them. It appears jQuery assumes the object is a DOM node when unbinding, and throws the following error:</p> <pre><code>Uncaught TypeError: Object #&lt;an Object&gt; has no method 'removeEventListener' </code></pre> <p>Even the part about being able to bind events to objects is, I believe, undocumented. So you have to be a little careful on this, as the following will not work when trying to clean up the event handler references for that object:</p> <p><strike></p> <pre><code>$(object).remove() $(object).unbind(..) </code></pre> <p></strike></p> <p>As a workaround, when cleaning up the Trip object, you can explicitly call <code>removeData</code> to do the job.</p> <pre><code>$(object).removeData(); </code></pre> <p>As I've already mentioned, it's getting knee-deep with jQuery's internals, so you might want to look at an alternative solution, or be wary that library upgrades can easily break your code, which is not very unlikely.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload