So the question is simply, if it's safe (wise) to declare method called "delete" in JavaScript. Example:
var request = { delete : function (url) { // Some code... } } request.delete('http://page.dev/users/1');
I've tested this in Firefox and it's functional, but just wondering if it could cause problems in some other browsers; or in general if is it a good practice.
Conclusion. delete is the only true way to remove an object's properties without any leftovers, but it works ~100 times slower if you are using delete in loops.
No. this is just a local reference to the object so deleting it does not make the object not exist. There is no way for an object to self destruct in this manner.
The delete keyword deletes both the value of the property and the property itself. After deletion, the property cannot be used before it is added back again. The delete operator is designed to be used on object properties. It has no effect on variables or functions.
The delete operator in JavaScript is used to delete an object's property. If it is used to delete an object property that already exists, it returns true and removes the property from the object.
According to the language specification:
Identifier Names are tokens that are interpreted according to the grammar given in the “Identifiers” section of chapter 5 of the Unicode standard, with some small modifications. An Identifier is an IdentifierName that is not a ReservedWord (see 7.6.1).
This means you cannot use delete
as a variable or function name, but you can use it as an object property name. This was not the case in the previous version of the specification, which is why most other answers are recommending you avoid using reserved words entirely. However, in ES5-compliant implementations, there should be no problem.
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