JavaScript object destructor method ???

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Eli

    #1

    JavaScript object destructor method ???

    Hi,

    Is there any way that I can define a destructor for an object in
    JavaScript that will be called automatically when object is discarded?


    <script type="text/javascript">
    MyOBJ.prototype =new Object();
    MyOBJ.prototype .constructor=My OBJ;
    function MyOBJ()
    {
    alert('Construc tor!');
    }
    MyOBJ.prototype .destructor=fun ction()
    {
    alert('Destruct or!');
    }

    var my_obj=new MyOBJ(); //<-- will show 'Constructor!'
    delete my_obj; //<-- will show 'Destructor!'
    </script>


    -thanks in advance, Eli

  • VK

    #2
    Re: JavaScript object destructor method ???

    Memory alloc/calloc in JavaScript works similar to Java. You don't need
    and you really cannot free the memory directly, because you don't have
    a direct memory access. You can remove a reference to a custom object
    by doing myObject = null;
    If it was the last reference to the instance, it gets marked as free to
    remove and it will be eventually removed by the garbage collector.

    Comment

    • Eli

      #3
      Re: JavaScript object destructor method ???

      But what if I want to delete all the references to an object?
      I got some references to the same object, and I want to delete the
      object totally in all references. Is it possible?

      -thanks, Eli

      Comment

      • VK

        #4
        Re: JavaScript object destructor method ???

        Sure! Just keep track of all references you are creating ;-) and don't
        make more references than it's necessary ;-)
        If the task could be solved globally by some simple trick, we would not
        hear about memory leaks (but we do).

        Comment

        • Lasse Reichstein Nielsen

          #5
          Re: JavaScript object destructor method ???

          "Eli" <eli.hen@gmail. com> writes:
          [color=blue]
          > But what if I want to delete all the references to an object?[/color]

          Feel free. That's not guaranteed to delete the object, though.

          ECMAScript makes no requirements on memory management, so there is no
          guarantee that any object is ever destroyed. Some implementations will
          indubiably use a grabage collector, while others will merely keep
          allocating until the page is unloaded.
          [color=blue]
          > I got some references to the same object, and I want to delete the
          > object totally in all references. Is it possible?[/color]

          Given only one reference, no.

          /L
          --
          Lasse Reichstein Nielsen - lrn@hotpop.com
          DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
          'Faith without judgement merely degrades the spirit divine.'

          Comment

          • Douglas Crockford

            #6
            Re: JavaScript object destructor method ???

            Eli wrote:
            [color=blue]
            > Is there any way that I can define a destructor for an object in
            > JavaScript that will be called automatically when object is discarded?[/color]

            No. Why do you think you need to?

            Comment

            Working...