I know that delete is a keyword in JavaScript. So I have this code (for example):
var user = {
create : function () {
// Create a user account
},
delete : function () {
// Delete a user account
}
};
The above works (barring older versions of IE), so my question is – is it a good idea. Obviously the call user.delete(); is much clearer to someone utilizing the code than something like user.delete_one();
Obviously keywords are important, but on a case by case basis is it alright (granted I don’t need legacy IE support) to use this method, or is there a better solution?
You code will work as expected, because you are not overwriting JS keyword. If you try to declare a keyword as variable or function name, JS will show error
SyntaxError: Unexpected token delete.It ‘s alright with the way you choose but don’t override JS keywords directly.