Javascript方法命名小写与大写 [英] Javascript Method Naming lowercase vs uppercase
问题描述
我主要是ASP.NET和C#的开发人员。我将变量命名为小写,我的方法以大写开头。但我研究的大多数javascript示例都有以小写字母开头的函数。为什么会这么重要?
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'); }
推荐答案
Javascript中的一个流行约定是大写构造函数(通常也被误称为类)。
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');
这个惯例很受欢迎,Crockford甚至把它包含在 JSLint 在可选项下 - 要求构造函数的初始上限:)
This convention is so popular that Crockford even included it in its JSLint under an optional — "Require Initial Caps for constructors" : )
任何不是构造函数通常以小写开头,并且 camelCased 。这种风格有些原生于Javascript; ECMAScript,例如(ECMA-262,第3版和第5版) - JavaScript和其他实现符合 - 完全遵循这个约定,在camelcase中命名内置方法 - Date.prototype.getFullYear
, Object.prototype.hasOwnProperty
, String.prototype.charCodeAt
等。
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.
这篇关于Javascript方法命名小写与大写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!