简体   繁体   中英

Check if a Typescript class has already been instantiated

I'm almost new to Typescript (and also to OOP) and I have a module like the following:

module SIMULATOR {
    export class myClass extends myBaseClass{
        constructor(){
        //Make a div and append it to body
        }
    }
}

Than i have an element with onClick function that call myClass like this:

$(button).on('click', ()=> {
    if(//class hasn't been instantiated yet){
        var myDiv = new SIMULATOR.myClass();
    }
    else{
        //do other stuff (eg: delete myClass instance)
    }
});

My first goal is to understand how i can check if myClass has been already instantiated.

Later i will destroy the created div with jquery and i would like to know how to "destroy" myClass instance too.

I apologize if the questions may seem silly: I'm a beginner:)

Since you're using jQuery :), here's how I would do this:

$(button).on('click', ()=> {
    var myDiv: SIMULATOR.myClass = $(button).data('myClass');
    if(myDiv){
        //do other stuff (eg: delete myClass instance)
    }
    else {
        // myDiv has not been instantiated
        myDiv = new SIMULATOR.myClass();
        $(button).data('myClass', myDiv)
    }
});

This actually stores the instance of the class in jQuery's data structure, in memory, not as a data- attribute. If the instance is in the data structure, you're good to go. If it's not, you create it and add it to the data structure.

To remove the instance from the data structure, use $(button).data('myClass', null); .

That won't dispose of the object, but will remove references of it so that the JavaScript engine will collect it as garbage.

This is something interesting I came across by chance.

console output

Below is the output from my browser console

> class Home {}
< undefined
> typeof Home
< "function"
> typeof (new Home)
< "object"

Based on this, you can check whether the class has been initialised by checking it's type. eg:

class Home {};

let val = new Home();


if (typeof val === "object") {
  // class initialised
}

It would be the same as checking it in regular javascript. You will need to see if that object is null or undefined

Typescript is great for helping you do the development but everything compiles down to plain javascript for run time.

using a locally scoped variable as your myDiv is it will never be already initialized. I would recommend moving it outside your inline function. John is correct that all you need to do is check for null or undefined so you can just put the variable name inside the if stagement as nulls and undefined equate to false.

var myDiv;
$(button).on('click', ()=> {
    if(!myDiv){
        myDiv = new SIMULATOR.myClass();
    }
    else{
        //do other stuff (eg: delete myClass instance)
    }
});

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM