admin管理员组

文章数量:1318563

I recently was quickly shortening some method names in a JavaScript file and ran into a problem when I converted one method name:

Before:

RefreshSevenDayGrid(){
    // some stuff
}

After:

7Day() {    
    // some stuff
}

I quickly discovered that the javascript no longer worked. I heard from several people that numbers should never be used for method or class names. Is there ever an exception to this?

I recently was quickly shortening some method names in a JavaScript file and ran into a problem when I converted one method name:

Before:

RefreshSevenDayGrid(){
    // some stuff
}

After:

7Day() {    
    // some stuff
}

I quickly discovered that the javascript no longer worked. I heard from several people that numbers should never be used for method or class names. Is there ever an exception to this?

Share Improve this question edited Oct 2, 2020 at 12:15 m02ph3u5 3,1617 gold badges41 silver badges57 bronze badges asked Jan 22, 2009 at 21:52 Bobby BorszichBobby Borszich 11.8k9 gold badges40 silver badges35 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

It tends to cause fits for language parsers. It sees a leading digit, so expects to begin reading a numeric literal, then barfs when it sees a letter. Even the algebraic convention is that a number to the left of a letter is a separate numeric literal with the space omitted, so 7x would be seen as two tokens.

Besides what Jeffrey Hantin said, there are numeric constants such as

3e7  // 3x10^7
40L  // C, C++, etc for a long integer
0x88 // hexadecimal

The general convention for identifiers which is used widely in most languages, is [S except for 0-9][S]* where S is some set of valid characters (A-Z, a-z, 0-9, sometimes _, $, or -) -- so the first character can't be a digit but the rest can.

本文标签: javascriptStarting Class or Method Names with Numberswhy notStack Overflow