开发者

Javascript Method Naming lowercase vs uppercase

开发者 https://www.devze.com 2022-12-08 17:17 出处:网络
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 lo

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 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.


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.


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.


A class should always start with a capital letter, because it is easier to read and use:

const data = new Data(); 

Using a capital letter in a function is also easier to read than using lowercase:

User.email 

And strings, int, etc, however, should always start with lowercase to prevent confusion within the code. There is no difference, but it will make it easier to read and understand.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消