views:

304

answers:

4

I am for the most part a developer in ASP.NET and C#. I name my variables starting in lowercase and my methods starting in uppercase. but most javascript examples I study have functions starting in lowercase. Why is this and does it matter?

function someMethod() { alert('foo'); }

vs

function SomeMethod() { alert('bar'); }
A: 

It honestly depends. Your first method is called Camel Coding, and is a standard used by Java and C++ languages, and taught a lot in CS.

The second is used by .NET for their classes and then the _camelCode notation used for private members.

I like the second, but that's my taste, which is what I think this depends on.

Kyle Rozendo
A: 

I like to think it's because "JavaScript" start's with "java", hence we like to code in the standard of java, while, in it :) At least, this is my reasoning.

I still follow this pattern to this day, even though I program in c# mostly.

It doesn't matter at all; pick which way is most readable for you and your team, and stick with that approach.

Noon Silk
A: 

the naming convention with the lowercase at the start is called camel case. The other naming convention with a capital at the start is named Pascal case.

The naming convention only matters for your readability. Pick a convention and remember to stick with it throughout your application.

Andrew Keith
+6  A: 

A popular convention in Javascript is to only capitalize constructors (also often mistakenly called "classes").

function Person(name) {
  this.name = name;
}
var person = new Person('John');

This convention is so popular that Crockford even included it in its JSLint under an optional — "Require Initial Caps for constructors" : )

Anything that's not a constructor usually starts with lowercase and is camelcased. This style is somewhat native to Javascript; ECMAScript, for example (ECMA-262, 3rd and 5th editions) — which JavaScript and other implementations conform to — follows exactly this convention, naming built-in methods in camelcase — Date.prototype.getFullYear, Object.prototype.hasOwnProperty, String.prototype.charCodeAt, etc.

kangax