Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is using the Js delete operator to remove a property from an object considered bad practice? [closed]

Tags:

javascript

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.

like image 382
Mix Master Mike Avatar asked Oct 29 '25 09:10

Mix Master Mike


2 Answers

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?

like image 124
Fritiof Rusck Avatar answered Oct 31 '25 23:10

Fritiof Rusck


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.

like image 37
Pablo Darde Avatar answered Oct 31 '25 22:10

Pablo Darde



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!