Javascript方法命名小写与大写

时间:2021-08-28 13:22:42

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?

我大部分是ASP.NET和C#的开发人员。我将变量命名为小写,我的方法以大写开头。但是我研究的大多数javascript示例都有以小写字母开头的函数。为什么会这样,这有关系吗?

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

vs

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

4 个解决方案

#1


56  

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

Javascript中的一个流行约定是仅对构造函数进行大写(也常常被错误地称为“类”)。

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" : )

这个惯例是如此受欢迎,以至于Crockford甚至将它包含在JSLint中的可选项下 - “需要构造函数的初始上限”:)

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.

任何不是构造函数的东西通常以小写字母开头并且是camelCased。这种风格有些原生于Javascript; ECMAScript,例如(ECMA-262,第3版和第5版) - JavaScript和其他实现符合 - 完全遵循这个约定,命名为camelcase中的内置方法 - Date.prototype.getFullYear,Object.prototype.hasOwnProperty,String。 prototype.charCodeAt等

#2


1  

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.

老实说,这取决于。您的第一种方法称为Camel Coding,是Java和C ++语言使用的标准,在CS中教授了很多。

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

第二个用于.NET的类,然后用于私有成员的_camelCode表示法。

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

我喜欢第二个,但这是我的口味,这是我认为这取决于。

#3


0  

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.

我喜欢认为这是因为“JavaScript”以“java”开头,因此我们喜欢​​用java标准编写代码,而在其中:)至少,这是我的推理。

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

到目前为止,我仍然遵循这种模式,即使我主要用c#编程。

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

根本没关系;选择哪种方式对您和您的团队最具可读性,并坚持使用该方法。

#4


0  

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.

在开头使用小写的命名约定称为camel case。在开始时使用大写的另一个命名约定命名为Pascal case。

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

命名约定仅对您的可读性有影响。选择一个约定并记住在整个应用程序中坚持使用它。

#1


56  

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

Javascript中的一个流行约定是仅对构造函数进行大写(也常常被错误地称为“类”)。

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" : )

这个惯例是如此受欢迎,以至于Crockford甚至将它包含在JSLint中的可选项下 - “需要构造函数的初始上限”:)

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.

任何不是构造函数的东西通常以小写字母开头并且是camelCased。这种风格有些原生于Javascript; ECMAScript,例如(ECMA-262,第3版和第5版) - JavaScript和其他实现符合 - 完全遵循这个约定,命名为camelcase中的内置方法 - Date.prototype.getFullYear,Object.prototype.hasOwnProperty,String。 prototype.charCodeAt等

#2


1  

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.

老实说,这取决于。您的第一种方法称为Camel Coding,是Java和C ++语言使用的标准,在CS中教授了很多。

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

第二个用于.NET的类,然后用于私有成员的_camelCode表示法。

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

我喜欢第二个,但这是我的口味,这是我认为这取决于。

#3


0  

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.

我喜欢认为这是因为“JavaScript”以“java”开头,因此我们喜欢​​用java标准编写代码,而在其中:)至少,这是我的推理。

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

到目前为止,我仍然遵循这种模式,即使我主要用c#编程。

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

根本没关系;选择哪种方式对您和您的团队最具可读性,并坚持使用该方法。

#4


0  

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.

在开头使用小写的命名约定称为camel case。在开始时使用大写的另一个命名约定命名为Pascal case。

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

命名约定仅对您的可读性有影响。选择一个约定并记住在整个应用程序中坚持使用它。