我什么时候应该用大写字母命名? [英] When should I name things with initial capital letters?
问题描述
我总是想知道何时使用带有大写字母而不是驼峰的标识符(例如,函数)。我总是用骆驼案例写我的JS:
I have always wondered when to use identifiers (for example, functions) with capital first letter instead of camel case. I always write my JS in camel case like this:
function doStuff() {}
var simpleVar = 'some stuff',
myAry = [],
myObj = {};
...但我知道我应该用首字母命名一些东西。我只是不知道这条规则适用的时候。希望有人能让我更清楚一点。
... But I know I am supposed to name some things with capital first letters. I just don't know WHEN this rule applies. Hope somebody can make things a bit clearer to me.
推荐答案
根据Javascript:好的部分一书,你应该当你需要通过new关键字构造对象时,只会大写函数名的第一个字符。
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.
这称为构造函数调用模式,继承的方式。
This is called "the Constructor Invocation Pattern", a way to inherits.
这篇关于我什么时候应该用大写字母命名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!