When removing a property from an object, I've used the delete operator in the past. But when I had my code reviewed by a person with a CS background, they wanted me to find another way of removing the property because it was an "expensive" task (not sure what that means). Since I'm self taught I wanted to ask if deleting in Js is something I should consider using as last resort? I've looked at other responses within SO like this but couldn't find anything that makes reference to this.
No, it's not considered bad practice. It just depends on what you want to do,
Read this thread: Garbage Collection and JavaScript "delete": Is this overkill/obfuscation, or a good practice?
The book "JavaScript, the Good Parts" by Douglas CrockFord says...
The delete operator can be used to remove a property from an object. It will remove a property from the object if it has one. It will not touch any of the objects in the proto- type linkage.
I think you can use the Delete operator without worries. There are many other ways to remove a property from an object, but all they are more expensive in my opinion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With