CarlosCarucce CarlosCarucce - 1 year ago 107
Javascript Question

ONDELETE object event in javascript

I have a modal object that creates DOM elements when its created.
I would like to know if its possible to remove the nodes whenever user calls 'delete' on the object. Something like:

var modal = function (options) {
var domHolder = null;

function build() {
domHolder = document.createElement("div");
//construct a modal window inside the element...

this.ondelete = function() {


//destruct the object and remove the nodes
var myModal = new modal();
delete myModal;

Answer Source

delete doesn't trigger the onDeleteEvent , delete is an operator;

The delete operator removes a property from an object.

You shoud call the ondelete method to remove the element from DOM.

var myModal = new modal();
myModal.ondelete() ;

JavaScript is automatically garbage collected; the object's memory will be reclaimed only if the Garbage Collectior decides to run and the object is eligible for that.

The delete operator has nothing to do with directly freeing memory (it only does indirectly via breaking references). See the memory management page for more details).