Javascript 方法命名小写与大写

发布于 2024-08-07 04:28:46 字数 245 浏览 4 评论 0原文

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

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

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

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'); }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

吻风 2024-08-14 04:28:46

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

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

这个约定非常受欢迎,以至于 Crockford 甚至将其包含在其 JSLint 中的一个可选选项 —“构造函数需要初始大写”下:)

任何不是构造函数的东西通常以小写开头并且camelCased< /a>。这种风格有点原生于 Javascript;例如,ECMAScript(ECMA-262,第 3 版和第 5 版)(JavaScript 和其他实现都遵循该约定)完全遵循此约定,以驼峰命名法命名内置方法 - Date.prototype.getFullYear, <代码>Object.prototype.hasOwnProperty、String.prototype.charCodeAt等。

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.

行至春深 2024-08-14 04:28:46

老实说,这取决于。您的第一个方法称为“骆驼编码”,它是 Java 和 C++ 语言使用的标准,并且在 CS 中教授了很多内容。

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

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

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.

偏爱自由 2024-08-14 04:28:46

以小写开头的命名约定称为驼峰式。另一种以大写开头的命名约定称为 Pascal case。

命名约定只影响您的可读性。选择一个约定,并记住在整个申请过程中遵守它。

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.

单调的奢华 2024-08-14 04:28:46

类应始终以大写字母开头,因为它更易于阅读和使用:

const data = new Data(); 

在函数中使用大写字母也比使用小写字母更易于阅读:

User.email 

然而,字符串、int 等应始终以小写字母开头防止代码中出现混乱。没有什么区别,但是会更容易阅读和理解。

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.

失去的东西太少 2024-08-14 04:28:46

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

直到今天我仍然遵循这种模式,尽管我主要使用 C# 进行编程。

这根本不重要;选择对您和您的团队来说最易读的方式,并坚持使用该方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文