26

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.

JW.
  • 50,691
  • 36
  • 115
  • 143

4 Answers4

44

You can follow this Google JavaScript Style Guide

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

Peter Krieg
  • 352
  • 1
  • 4
  • 10
Pavel Hodek
  • 14,319
  • 3
  • 32
  • 37
  • 1
    your link recommends saving filenames like `filenameslikethis.js`. Do you know why everything is in lower case? I think that looks like it's hard to read. – ayjay Jun 26 '14 at 18:06
  • 1
    There is written " ... and should contain no punctuation except for `-` or `_` (prefer `-` to `_`)". So it also recommends `file-name-like-this.js` – Pavel Hodek Sep 11 '14 at 08:49
  • What if a variable contains a class? `var ConstructorClass = classMap['className']` or `var constructorClass = classMap['className']`? – Tomasz Gałkowski Nov 21 '16 at 13:17
  • @ayjay `file-name-like-this.js` seems more readable to me than `FileNameLikeThis.js`. You could do a `File-Name-Like-This.js`, but I don't see any value in using capital letters there. – Sinjai Jul 18 '17 at 16:34
3

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

Austin737
  • 675
  • 1
  • 8
  • 15
1

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.

Justin Johnson
  • 30,978
  • 7
  • 65
  • 89
  • Sorry, no, that’s not true at all. Conventions aren’t rules. The language won't enforce a capitalization rule, but there are still common conventions (trends and expectations of actual developers) – Alan H. May 02 '22 at 17:46
  • @AlanH. Hi there. You're commenting on a an answer that is 12+ years old. The JS community has undergone huge changes and growth since then, but this was true at the time of posting. JS was very wild-west in 2009 (Node was released this year and chrome was barely a year old). – Justin Johnson May 13 '22 at 19:21
0

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.

scunliffe
  • 62,582
  • 25
  • 126
  • 161