5

I'm adopting a large mish-mash of Javascript / jQuery code. I'd like to move it to Backbone.js but as an intermediary, I'm trying to namespace it so that it is sligthtly more modular.

I am wondering if there is a standard for namespaces for Javascript. I was going to do something like this:

var arc={
  name:"Spock",

  nav:function(){
    //alert('name' + this.name);
    obj=get_nav_obj();
    get_to_api_v2(obj);
  }

};

Should arc be capitalized? I tend to see capitalized but since rest of site is in Ruby, lowercase might be more consistent.

thx

timpone
  • 19,235
  • 36
  • 121
  • 211

3 Answers3

7

In javascript, camelCase is used for regular identifiers. CAPSLOCKS is used for "constants". ProperCase is used for object constructors (indicating they should always be invoked using new).

Community
  • 1
  • 1
jbabey
  • 45,965
  • 12
  • 71
  • 94
3

There is no absolute standard for captilization in Javascript. The over whelming majority of javascript though prefers camel casing. In short camel casing has the first letter of an identifier in lower case and then capitalizes the start of every subsequent word in a name. Here arc is a single word and hence all lower is appropriate.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • the accepted answer feels a lot "more right" in this case, though I agree with you there is no absolute standard – 1nfiniti Mar 15 '16 at 01:10
2

This come down to personal preference. Our code best practice define that only function constructor have to be capitalised. For me the instance of an object is then lowercase.

Do google search for "javascript code guidleine" and see what other are doing

elmuchacho
  • 424
  • 3
  • 8