Why is $ a valid function identifier?

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

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

+4
source share
4 answers

From the ECMA standard (section 7.6)

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

+10
source

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

If you read section 7.6, you will see part of the identifier syntax.

Essentially, the characters that can be used are defined:

 Identifier :: IdentifierName but not ReservedWord IdentifierName :: IdentifierStart IdentifierName IdentifierPart IdentifierStart :: UnicodeLetter $ _ \ UnicodeEscapeSequence IdentifierPart :: IdentifierStart UnicodeCombiningMark UnicodeDigit UnicodeConnectorPunctuation <ZWNJ> <ZWJ> 
+1
source

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

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

0
source

Because this is ECMA-262 indicates (see section 7.6)

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

0
source

Source: https://habr.com/ru/post/1337005/


All Articles