简体   繁体   中英

How to delete Javascript created class?

I have some code:

let new_user = new User({
    username: 'someone'
}, isValid => {
    if (!isValid) {
        // I need to delete this user class
    }
})

I tried to do new_user = null , but that doesn't work

I tried to do new_user = null , but that doesn't work

Yes, it does, provided new_user is the only reference to that User object. Once all references to an object have been released, the object is eligible for garbage collection. (The value doesn't have to be null , specifically; just assigning anything else to new_user releases the reference it used to hold to the object.)

(The details of when and how that happens are up to the implementation of the JavaScript engine, but that's not important and you can't observe it in your code.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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