0

Currently I'm trying to understand source code of Backbone.js and noticed that the main Backbone property/object is first letter capitalized:

var root = (typeof self == 'object' && self.self === self && self) ||
            (typeof global == 'object' && global.global === global && global);

  // Set up Backbone appropriately for the environment. Start with AMD.
  if (typeof define === 'function' && define.amd) {
    define(['underscore', 'jquery', 'exports'], function(_, $, exports) {
      // Export global even in AMD case in case this script is loaded with
      // others that may still expect a global Backbone.
      root.Backbone = factory(root, exports, _, $);
});

I don't understand why it's not just root.backbone. I've heard that people name constructors with first letter capitalized but in the case we have factory method.

So, when we aren't dealing with a library and write simple code we don't do such capitalization(as far as I see on the tutorials) and we don't name objects which were created with constructors in such way:

var person = new Person("John", "Doe", 50, "blue") //not var Person

I think the answer to the question can help a lot of newcomers to understand js script code.

Artem Malchenko
  • 2,320
  • 1
  • 18
  • 39
  • 3
    Unfortunately, while this may seem like a good question on the surface, it's not really a question that has a technical answer. The JavaScript language doesn't have any specific requirement on naming conventions, which makes the specific conventions used a matter of opinion. Therefore, multiple answers to this question could all be correct, making the question a poor fit for this format. – Claies May 18 '18 at 20:10
  • 1
    Unfortunately, a question being helpful to newcomers does not automatically make a question on-topic. Off-topic questions are off-topic, even if they might be helpful. –  May 18 '18 at 20:15

2 Answers2

1

I don't understand why it's not just root.backbone.

There isn't any specific reason, but naming libraries and other global objects with capitalized names is a common convention. It reduces the risk that the names will conflict with local variables, which are typically named in lowercase.

var person = new Person("John", "Doe", 50, "blue") //not var Person

In this case, Person already exists as a global variable! Naming the local variable Person would conflict with that global variable, and the code would actually not work at all. (The Person in new Person would be interpreted as referring to the local variable, which is not yet initialized.)

0

The interpreter doesn't care whether or where you use uppercase or lowercase letters. There's no technical difference between the two, and no one has yet passed a law in any jurisdiction I'm aware of saying that you have to use one style or another. So at the end of the day, which to use is up to personal preference. You, and the Backbone.js authors, are free to use identifiers like Backbone or backbone or bAcK__bOnE.

Most languages have stylistic conventions that the community of developers in that language have tended toward. Methods in Java tend to be lowerCamelCased() while methods in C# tend to be UpperCamelCased(). The C++ standard library favors lowercase_with_underscores. And so on. Of course some authors in all of these languages deviate from what most people in the community would think of as "the norm." You're free to do that too, although there are some concrete readability and maintainability advantages to following the prevailing community style (or your company/organization style if applicable).

In JavaScript things are a little muddled, because almost everything is an object. In this particular case, I guess the Backbone.js authors decided that since the Backbone object in question represents the entry point to the Backbone API, it's more class-like than object-like and to emphasize that, they gave it a capital B.

TypeIA
  • 16,916
  • 1
  • 38
  • 52