Why is “$” a valid function identifier? [duplicate]

不想你离开。 提交于 2019-12-12 11:22:29

问题


Possible Duplicates:
Can someone explain the dollar sign in Javascript?
Why would a javascript variable start with a dollar sign?

Why is it that I can assign a function to $ in Javascript, but not # or ^ ?


回答1:


From the ECMA standard (Section 7.6)

The dollar sign ($) and the underscore (_) are permitted anywhere in an IdentifierName.




回答2:


The reason is because JavaScript is part of the ECMA-262 standard.

If you read section 7.6 you'll see the part about Identifier syntax.

Essentially the characters that can be used are defined by:

Identifier ::
  IdentifierName but not ReservedWord

IdentifierName ::
  IdentifierStart
  IdentifierName IdentifierPart

IdentifierStart ::
  UnicodeLetter
  $
  _
  \
  UnicodeEscapeSequence

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



回答3:


If I understand your question, it's simply because the Javascript interpreter ignores $ as any type of special character. You can assign a function to a and you can assign one to $.

Much like an underscore, it treats $ as any "normal" character.




回答4:


Because that is what ECMA-262 specifies (see section 7.6)

Identifiers must match this RegEx: [a-zA-Z_$][0-9a-zA-Z_$]*



来源:https://stackoverflow.com/questions/4795551/why-is-a-valid-function-identifier

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!