ECMAScript 6 class destructor

前端 未结 5 1161
离开以前
离开以前 2020-12-02 16:23

I know ECMAScript 6 has constructors but is there such a thing as destructors for ECMAScript 6?

For example if I register some of my object\'s methods as event liste

5条回答
  •  心在旅途
    2020-12-02 17:17

    Is there such a thing as destructors for ECMAScript 6?

    No. EcmaScript 6 does not specify any garbage collection semantics at all[1], so there is nothing like a "destruction" either.

    If I register some of my object's methods as event listeners in the constructor, I want to remove them when my object is deleted

    A destructor wouldn't even help you here. It's the event listeners themselves that still reference your object, so it would not be able to get garbage-collected before they are unregistered.
    What you are actually looking for is a method of registering listeners without marking them as live root objects. (Ask your local eventsource manufacturer for such a feature).

    1): Well, there is a beginning with the specification of WeakMap and WeakSet objects. However, true weak references are still in the pipeline [1][2].

提交回复
热议问题