javascript - Destroying object -


there nice rich text editor scribe. in app have multiple editable divs on page, , on every focus event on particular div want turn div editable mode scribe. on blur event destroy instance of scribe, due fact having seperate instance every div high memory consuming.

this amd module attaches scribe instance given dom element:

define(['scribe'], function (scribe) {      'use strict';      function init(element) {         var scribe = new scribe(element, { allowblockelements: true });         //...         }      return {         attacheditor: init     }; }); 

this piece of code makes use of module above:

$('.editable').focus(function(e) {   editormodule.attacheditor(e.target); }); 

and can't figure out how detach or destroy existing scribe instance. tried code:

define(['scribe'], function (scribe) {      'use strict';     var scribe;     function init(element) {         scribe = new scribe(element, { allowblockelements: true });         //...         }      function destroy() {         scribe = null;     }      return {         attacheditor: init,         detacheditor: destroy,     }; }); 

hoping assigning null variable tell gc remove object. doubt way - when take memory snapshot in chrome can still see scribe instance.

any idea how handle this?

you don't need delete object yourself. if set of variables referencing null, automatically deleted. object still there because there other references in code somewhere. if can't find other references, can make empty object, before setting scribe null this:

function destroy() {    for(var in scribe)    {        delete scribe[i];    }    scribe = null; } 

that @ least free memory. way rid of set variables referencing null.


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -