从构造函数创建新对象时如何包含或检测它的名称

我有一个包含调试/日志代码以及自毁方法的构造函数


我试图在互联网上找到有关如何在创建过程中检测新对象名称的信息,但是我发现的唯一建议是将名称作为属性传递。


例如


var counter = {}

counter.a =new TimerFlex({debug: true, timerId:'counter.a'});

我发现没有必要将counter.a作为timerId:'counter.a'传递,应该有一种本机方法从构造函数或新对象实例中检测名称。


我正在寻找类似ObjectProperties('name')的返回counter.a的东西,所以我不需要手动将其作为属性包含在内。


添加更多信息

@CertainPerformance我需要的是区分并行运行或嵌套运行的不同对象,因此可以在控制台中看到。


counter.a data...

counter.b data...

counter.a data...

counter.c data... etc

这些对象也只有一个唯一的名称,没有引用为counter.a = counter.c


另一个功能或TimerFlex是一种自我破坏的方法


 this.purgeCount = function(manualId) {


    if (!this.timerId && manualId) { 

      this.timerId = manualId;

      this.txtId =  manualId;

    } 


    if (this.timerId) {

      clearTimeout(this.t);

      this.timer_is_on = 0;

      setTimeout (  ()=> { console.log(this.txtId + " Destructed" ) },500);

      setTimeout ( this.timerId +".__proto__ = null", 1000);

      setTimeout ( this.timerId +" = null",1100);

      setTimeout ( "delete " + this.timerId, 1200);


    } else {

      if (this.debug) console.log("timerId is undefined, unable to purge automatically");

    }

  }

尽管我还没有关于此构造方法的演示,但这与我先前的问题有关,如何使相同的Javascript自我调用功能模式在并行运行超过一次而不覆盖值?


杨__羊羊
浏览 124回答 3
3回答

繁星coding

在JavaScript中,您可以通过与字典相同的符号来访问对象实例的属性。例如:counter['a']。如果您打算counter.a在新TimerFlex实例中使用它,为什么不通过counter?counter.a = new TimerFlex({debug: true, timerId: counter});// Somewhere within the logic of TimerFlex...// var a = counter.a;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript