Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Method Naming lowercase vs uppercase

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'); } 
like image 411
Bobby Borszich Avatar asked Oct 14 '09 05:10

Bobby Borszich


People also ask

Should function names be capitalized JS?

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.

Should methods start with capital?

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.

What are the naming conventions for a function used in JavaScript?

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.


1 Answers

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.

like image 131
kangax Avatar answered Oct 12 '22 12:10

kangax