Why is "$" a valid function identifier?

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.


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.

链接地址: http://www.djcxy.com/p/94958.html

上一篇: 是jquery $(this)。 与$相同吗?

下一篇: 为什么“$”是一个有效的函数标识符?