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'); }
According to the book "Javascript: the good parts", you should only capitalise the first character of the name of a function when you need to construct the object by "new" keyword.
Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized. Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter. Internal words start with capital letters.
Naming Conventions Variable and function names written as camelCase. Global variables written in UPPERCASE (We don't, but it's quite common) Constants (like PI) written in UPPERCASE.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With