SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Kill instance of function with it's method

    I want to know if there is a way to kill an instance of class by it's method i tried the following code but it's not working.

    Code:
    function someFunc() {
    
    this.destroy = function() {
    this = null;
    }
    } var newInstance = new someFunc(); newInstance.destroy();
    any suggestions?
    NOTELAY.COM - Have a nice noty day
    ETA CONSULTING

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,826
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Have you tried:

    delete this;
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why newInstance.destroy() when you can simply do newInstance = null?

  4. #4
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @felgall: i just try delete this; but no effect... instance still exists.

    @Pepejeria: well there is some logic method of the instance that tests some variables and decides if the instance should be killed or should leave it alone
    NOTELAY.COM - Have a nice noty day
    ETA CONSULTING

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,826
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Have you tried using the delete followed by the null assignment as each removes a different part of the object and you sometimes need both to get rid of the whole thing (depending on how it was defined to start with).
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  6. #6
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That wont work, you will simply need to have a method in the object that returns a boolean to tell you if you need to delete it or not.
    Code:
    function Car() {
        
    }
    
    Car.prototype.finalize = function() {
        if(bla) return true;
    }
    
    var myCar = new Car();
    
    if(myCar.finalize()) myCar = null;

  7. #7
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by psokarovski View Post
    I want to know if there is a way to kill an instance of class by it's method i tried the following code but it's not working.
    You cannot "destroy" an object in javascript, not matter from its own method or from somewhere else. Object is destroyed automatically by javascript interpreter if there are no variables that refer to it. You don't have any control over when and how this happens.

  8. #8
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    stereofrog, setting the instance variable to null will destroy the instance.

  9. #9
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ooo i think i understand now why i can't use this = null; because this refers to the object and you can't destroy the object, you need to set to null the variable that is referring to the object.
    NOTELAY.COM - Have a nice noty day
    ETA CONSULTING

  10. #10
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pepejeria View Post
    stereofrog, setting the instance variable to null will destroy the instance.
    Only if this variable is the only reference to that instance. If the instance is referred somewhere else it won't be destroyed.

    Quote Originally Posted by psokarovski
    you can't destroy the object, you need to set to null the variable that is referring to the object.
    Exactly.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •