为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

杨__羊羊 2021-04-25 12:19:44
我有一个包含调试/日志代码以及自毁方法的构造函数我试图在互联网上找到有关如何在创建过程中检测新对象名称的信息,但是我发现的唯一建议是将名称作为属性传递。例如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自我调用功能模式在并行运行超过一次而不覆盖值?
查看完整描述

3 回答

?
繁星coding

TA贡献1797条经验 获得超4个赞

在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;


查看完整回答
反对 回复 2021-05-06
  • 3 回答
  • 0 关注
  • 123 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信