以数字开头的类或方法名称,为什么不呢?
我最近在快速缩短 JavaScript 文件中的一些方法名称,并在转换一个方法名称时遇到了问题:
之前:
RefreshSevenDayGrid(){
// some stuff
}
之后:
7Day() {
// some stuff
}
我很快发现 JavaScript 不再起作用。 我从几个人那里听说数字不应该用于方法或类名称。 这有例外吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它往往会导致语言解析器的不适。 它看到一个前导数字,因此期望开始读取数字文字,然后在看到字母时呕吐。 即使代数约定是字母左侧的数字是一个单独的数字文字,省略了空格,因此 7x 将被视为两个标记。
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.
除了 Jeffrey Hantin 所说的之外,还有一些数字常量,例如
在大多数语言中广泛使用的标识符的一般约定是
[S except for 0-9][S]*
其中 S 是某个集合有效字符(AZ、az、0-9,有时是 _、$ 或 -)——因此第一个字符不能是数字,但其余字符可以。Besides what Jeffrey Hantin said, there are numeric constants such as
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.