Search code examples
javascriptcoding-stylecapitalization

Capitalization convention for JavaScript objects


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.


Solution

  • You can follow this Google JavaScript Style Guide

    In general, use functionNamesLikeThis, variableNamesLikeThis, ClassNamesLikeThis, EnumNamesLikeThis, methodNamesLikeThis, and SYMBOLIC_CONSTANTS_LIKE_THIS.