最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Overwriting Javascript Keywords - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question asked Oct 11, 2012 at 13:25 FluidbyteFluidbyte 5,21010 gold badges49 silver badges79 bronze badges 3
  • 3 You're not overwriting a keyword, but defining an object property of the same name. delete will continue to work as usual. – Sirko Commented Oct 11, 2012 at 13:28
  • 1 Avoid reserved words. And this one isn't really obvious (delete generally does something very different from what you can do with a function here). – Denys Séguret Commented Oct 11, 2012 at 13:28
  • 1 @Sirko - That was my thought. I've never had any issues in the past doing this as long as I'm not redefining. – Fluidbyte Commented Oct 11, 2012 at 13:29
Add a comment  | 

4 Answers 4

Reset to default 7

You can do it like this:

var user = {
   create : function () {
       // Create a user account
   },

   'delete' : function () {
       // Delete a user account
   }
};

Or by using double quotes "

Don't attempt to overwrite keywords. IMO this is bad practice and would be very confusing for another developer. Rather than having a delete function you can simply rename it to remove

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.

When you are using an object literal, then a property name can be any of the following:

IdentifierName
StringLiteral
NumericLiteral

StringLiteral and NumericLiteral should be clear. What exactly is a IdentifierName?

Lets have a look at section 7.6 of the specification:

IdentifierName ::
     IdentifierStart
     IdentifierName *IdentifierPart*

IdentifierStart ::
     UnicodeLetter
     $
     _
     \ UnicodeEscapeSequence

IdentifierPart ::
     IdentifierStart
     UnicodeCombiningMark
     UnicodeDigit
     UnicodeConnectorPunctuation
     <ZWNJ>
     <ZWJ>

So, an IdentifierName really is any character sequence as described above. Whether is a reserved word does not matter.

The names you can use for variable and function names are called Identifiers and are defined as:

Identifier ::
     IdentifierName but not ReservedWord

You see, reserved words are explicitly excluded as possibilities for identifiers, but not for object properties.

However, you never know how "good" a parser is and if it adheres to all rules. Additionally, linting tools such as JSHint will usually warn you of the use of a reserved keyword, despite the fact that it is valid. To be on the safe side, you should put such words in quotes and even use bracket notation to access it:

var foo = {'delete': ... }
foo['delete'] = ....;

If this is too cumbersome, just don't use a reserved word as property name. For example, instead of delete, you could use remove.

发布评论

评论列表(0)

  1. 暂无评论