Discuss Scratch
- Discussion Forums
- » Advanced Topics
- » A guide to "delete this;" in JS classes
- Redstone1080
-
Scratcher
1000+ posts
A guide to "delete this;" in JS classes
Consider the following code:
What would ‘delete’ do in this case? If you did
would accessing myInstance produce an error?
Nope. It just pretty much creates an instance of an empty class.
However, static methods and fields still work, so if you want to create a class that is only usable through static methods and fields, you can use this method (although it probably isn't the best).
Here's the full code I tested this with:
class MyJSClass { constructor() { delete this; } }
What would ‘delete’ do in this case? If you did
const myInstance = new MyJSClass();
Nope. It just pretty much creates an instance of an empty class.
However, static methods and fields still work, so if you want to create a class that is only usable through static methods and fields, you can use this method (although it probably isn't the best).
Here's the full code I tested this with:
class MyClass { static doesThisWork = false; constructor() { delete this; } } const myInstance = new MyClass(); console.log(myInstance.doesThisWork) console.log(MyClass.doesThisWork)
Last edited by Redstone1080 (Oct. 9, 2022 22:34:22)
- TrustyRoo
-
New Scratcher
49 posts
A guide to "delete this;" in JS classes
This is pretty logical, since you are creating the instance as a constant, even if you call the self destruct call in the class, it will not fully destroy itself because it is a constant at the end of the day and those cannot be destroyed before the end of the program. If it where not a constant, I would assume it would fully self destruct.
- Redstone1080
-
Scratcher
1000+ posts
A guide to "delete this;" in JS classes
(#2)I tried it with let and var and I got the same result. So it isn't a constants thing.
This is pretty logical, since you are creating the instance as a constant, even if you call the self destruct call in the class, it will not fully destroy itself because it is a constant at the end of the day and those cannot be destroyed before the end of the program. If it where not a constant, I would assume it would fully self destruct.
Anyways, here's some more testing. This is a complete example that you can run anywhere and works.


Last edited by Redstone1080 (Oct. 9, 2022 23:52:29)
- uwv
-
Scratcher
1000+ posts
A guide to "delete this;" in JS classes
(#3)this example is pretty flawed. you can't access static values in javascript using an instance, delete this doesn't really do anything. if you want to delete a value in javascript you have to set all references to either null or undefined(#2)I tried it with let and var and I got the same result. So it isn't a constants thing.
This is pretty logical, since you are creating the instance as a constant, even if you call the self destruct call in the class, it will not fully destroy itself because it is a constant at the end of the day and those cannot be destroyed before the end of the program. If it where not a constant, I would assume it would fully self destruct.
Anyways, here's some more testing. This is a complete example that you can run anywhere and works.

- Redstone1080
-
Scratcher
1000+ posts
A guide to "delete this;" in JS classes
Oh. I didn't know that. Thank you for this~snipped quotes~
- Discussion Forums
- » Advanced Topics
-
» A guide to "delete this;" in JS classes


