JavaScript 对象的大写约定
我知道这个问题没有答案,但我很想知道其他人的想法。
在像 Java 这样的语言中,类以大写字母开头,对象以小写字母开头是一种惯例。但是 JavaScript 呢?一切都是对象?
我见过一些人建议仅将被视为类的对象大写;即具有原型的函数对象,旨在与 new 运算符一起使用。这些对象的实例将被小写。
这听起来合乎逻辑。但是,对于只有一个实例的“全局对象”,您该怎么办呢?大多数人似乎都会将这些大写(例如,Math 或 Ext.History)。直觉上这感觉是对的,但很难用一致的规则来证明它的合理性。
那么用作命名空间的对象又如何呢?这些似乎遍布整个地图:YUI、Ext.util、jQuery 等。
请为你内心深处的宗教信仰提供世俗的合理化解释。意见。
I know this question has no answer, but I'm curious to know what other people think.
In a language like Java, it's a convention to begin classes with capital letters, and objects with lowercase letters. But what about JavaScript, where everything is an object?
I've seen some people suggest capitalizing only objects that are treated as classes; i.e. function objects with a prototype, that are intended to be used with the new operator. Instances of those objects would be lowercased.
That sounds logical. But what do you do about "global objects", where there's only one instance? Most people seem to capitalize these (for example, Math or Ext.History). This intuitively feels right, but it's hard to justify it with a consistent rule.
And what about objects that are used as namespaces? These seem to be all over the map: YUI, Ext.util, jQuery, etc.
Please provide secular rationalizations for your heart-felt religious views.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以按照此 Google JavaScript 样式指南
一般情况下,使用 functionNamesLikeThis、variableNamesLikeThis、ClassNamesLikeThis、EnumNamesLikeThis 、methodNamesLikeThis 和 SYMBOLIC_CONSTANTS_LIKE_THIS。
You can follow this Google JavaScript Style Guide
In general, use functionNamesLikeThis, variableNamesLikeThis, ClassNamesLikeThis, EnumNamesLikeThis, methodNamesLikeThis, and SYMBOLIC_CONSTANTS_LIKE_THIS.
正如 Douglas Crockford 所建议的:
“必须与 new 前缀一起使用的构造函数应以大写字母开头。如果省略了所需的 new,JavaScript 既不会发出编译时警告,也不会发出运行时警告。如果new 缺失,因此大写约定是一个重要的防御措施。”
https://www.crockford.com/code.html
As recommended by Douglas Crockford:
"Constructor functions that must be used with the new prefix should start with a capital letter. JavaScript issues neither a compile-time warning nor a run-time warning if a required new is omitted. Bad things can happen if new is missing, so the capitalization convention is an important defense."
https://www.crockford.com/code.html
约定就是没有约定。做你想做的事,保持一致。我建议遵循 Java 风格并忽略库的任何约定(
dojo
、Ext
、YUI
、$
等)您碰巧正在使用以下内容。The convention is that there is no convention. Do what you want, just be consistent. I suggest follow Java style and ignore whatever convention the library (
dojo
,Ext
,YUI
,$
, etc) you happen to be using is following.我同意定义“类”(使用空引号)的函数的大写,这些函数稍后将使用 new 运算符进行实例化。
但仅此而已。全局对象只是全局的。将它们命名为您想要的名称。
我要确保的是它们是唯一的并且具有足够的描述性,以至于以后不会被其他开发人员意外覆盖。
I agree with the capitalization of functions that define "classes" (air-quotes used) that in turn will be instanciated later using the new operator.
But that's it. Global objects are just global. Name them what you want.
All I would make sure is that they are unique and descriptive enough that they won't be overwritten accidentally by another developer at a later date.