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 badges2 Answers
Reset to default 7It 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
版权声明:本文标题:javascript - Starting Class or Method Names with Numbers, Why Not? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742049660a2417998.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论