简体   繁体   English

检查一个Typescript class是否已经被实例化

[英]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:我几乎是 Typescript(以及 OOP)的新手,我有一个如下所示的模块:

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:比我有一个元素 onClick function 调用 myClass 是这样的:

$(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.我的第一个目标是了解如何检查 myClass 是否已经实例化。

Later i will destroy the created div with jquery and i would like to know how to "destroy" myClass instance too.稍后我将用 jquery 销毁创建的 div,我也想知道如何“销毁”myClass 实例。

I apologize if the questions may seem silly: I'm a beginner:)如果问题看起来很愚蠢,我深表歉意:我是初学者:)

Since you're using jQuery :), here's how I would do this: 由于您使用的是jQuery :),因此,我将执行以下操作:

$(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. 这实际上将类的实例存储在jQuery的数据结构中的内存中,而不是作为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); 要从数据结构中删除实例,请使用$(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. 那不会处理该对象,但是会删除它的引用,以便JavaScript引擎将其作为垃圾收集。

This is something interesting I came across by chance.这是我偶然遇到的有趣的事情。

console output控制台 output

Below is the output from my browser console下面是来自我的浏览器控制台的 output

> 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.基于此,您可以通过检查其类型来检查class是否已被初始化。 eg:例如:

class Home {};

let val = new Home();


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

It would be the same as checking it in regular javascript. 它将与在常规javascript中检查它相同。 You will need to see if that object is null or undefined 您将需要查看该对象是否为nullundefined

Typescript is great for helping you do the development but everything compiles down to plain javascript for run time. Typescript非常适合帮助您进行开发,但是所有内容都可以在运行时编译为纯JavaScript。

using a locally scoped variable as your myDiv is it will never be already initialized. 使用本地范围的变量作为myDiv永远不会被初始化。 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. John是正确的,您所需要做的就是检查null或undefined,因此您可以将变量名称放入if阶段中,为null和undefined等于false。

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

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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