开发者

Why is "$" a valid function identifier? [duplicate]

开发者 https://www.devze.com 2023-02-06 10:51 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicates: Can someone explain the dollar sign in Javascript?
This question already has answers here: Closed 12 years ago.

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 ^ 开发者_运维知识库?


From the ECMA standard (Section 7.6)

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


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

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


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>


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消