简体   繁体   English

如何删除使用Object.defineProperty添加到类实例的属性?

[英]How to remove a property added to a class instance with Object.defineProperty?

It seems I can't just delete it, check it: 看来我不能只删除它,请检查它:

 class classy { constructor() { this.a = "I am classy"; } } var o = new classy(); Object.defineProperty(o, "b", {"get":()=>"hello"}); console.log("testing ob = " + ob); delete ob; console.log("deleted ob = " + ob); 

The default value of configurable is false so you should set it to true to be able to delete that property.. configurable的默认值为false因此您应该将其设置为true以能够删除该属性。

 class classy { constructor() { this.a = "I am classy"; } } var o = new classy(); Object.defineProperty(o, "b", { "get": () => "hello", configurable: true }); delete ob; console.log(ob) 

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

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