Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to declare a delete method in JavaScript

Tags:

javascript

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.

like image 374
VxMxPx Avatar asked Sep 13 '13 19:09

VxMxPx


People also ask

Should I use Delete in JavaScript?

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.

Can an object delete itself JS?

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.

What is delete keyword in JavaScript?

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.

What is the function of delete operator?

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.


1 Answers

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.

like image 122
bfavaretto Avatar answered Oct 11 '22 11:10

bfavaretto