0

Is there any way to delete a object from its method.

Let me explain a bit elaborately. I have a JS class called "Test" and I create a new instance of this class and assign it to a variable as below.

var Test = function()
{

}

Test.prototype =  
{
    printLog: function()
    {
        // Print some values
    },

    destroy: function()
    {
      //Here I want to delete **this** Test object.
    }
}

var a = new Test();

Now I want to delete the newly created Test class object which is assigned in variable a by invoking a.destroy as below.

a.destroy();
console.log(a);  //It should print null instead on object code.

After call the destroy() method, variable a value should be printed as null in console log.

Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
Kather
  • 95
  • 8
  • have you tried to assign `null` value to the element (`this = null;`) ? Maybe what I say is stupid but I wonder if it works or not. – AymDev Feb 02 '17 at 10:21
  • 1
    It doesn't, invalid left hand assignment. It only works if you namespace it. – Shilly Feb 02 '17 at 10:22
  • 1
    EcmaScript 6 does not specify any garbage collection semantics at all, so there is nothing like a "destruction" either. Source: http://stackoverflow.com/questions/29333017/ecmascript-6-class-destructor – Robert Feb 02 '17 at 10:23
  • The easiest way is just not using a method for it and just overwriting the variable a, but you can't do that from within a alas. – Shilly Feb 02 '17 at 10:25
  • I found these 2 questions if it can help: [Completely destroy JS object](http://stackoverflow.com/questions/30846899/how-to-completely-destroy-a-javascript-object) and [this one talking about namespace](http://stackoverflow.com/questions/10246215/how-to-destroy-javascript-object) – AymDev Feb 02 '17 at 10:25
  • I don't really now if it helps (I never tried), but take a look at delete method in JavaScript: http://adripofjavascript.com/blog/drips/the-delete-operator-in-javascript.html – Robert Feb 02 '17 at 10:26
  • delete removes properties from objects, it doesn't remove objects themselves. if 'a' was part of an object, then you could use `delete objectName.a`, but that still won't work from within a, since the 'this' reference of the 'a' instance, doesn't have a way to know that it's saved in the variable named 'a'. – Shilly Feb 02 '17 at 10:29

2 Answers2

0

Why don't you simply write a = null; instead of a.destroy(); ??

Inside of your destroy method, you can't use delete this because this isn't a property but an object. And it looks like you can't write this = null; neither. But as I said, I don't really think you need a method for making your object null :)

  • Thanks for your reply. Actually I'm creating a component (example java script window). When the user close a window, close() method will be invoked, from where I have to nullify the object. – Kather Feb 21 '17 at 14:03
0

One way you can use the destroy method is by using a namespace AND by hardcoding the variable name and sending both as parameters to destroy. But imho, this makes things just more complicated. So var a = null; is my preferred solution;

But if you opt for the destroy() approach, it's probably better to have the destroy method on the namespace instead of in the prototype of the class.

var Test = function() {

};
Test.prototype = {
    printLog: function() {
        // Print some values
    },

    destroy: function( namespace, instanceName ) {
        namespace[instanceName] = null;
    }
};
var instances = {
    'a' : new Test()
};
console.log(instances);
instances.a.destroy( instances, 'a');
console.log(instances);

///////////////////////////

var Test = function() {};
Test.prototype = {
    printLog: function() {
        // Print some values
    }
};
var Namespace = function() {};
Namespace.prototype = {
    'add' : function( name, obj ) {
        this[name] = obj;
    },
    'destroy' : function( name ) {
        delete this[name];
    }
};
var ns = new Namespace();
ns.add( 'a', new Test() );
console.log(ns);
ns.destroy('a');
console.log(ns);
Shilly
  • 8,511
  • 1
  • 18
  • 24