TypeScript - 使用 PascalCasing 或 camelCasing 作为模块名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29601960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeScript - Use PascalCasing or camelCasing for module names?
提问by Vadorequest
I'm wondering if I should use PascalCasing
or camelCasing
for my modules names, so far I've always used PascalCasing, i.e: Ayolan.Engine.Rendering
instead of Ayolan.engine.rendering
(I keep PascalCasing for the container, since I want the global object name to be Ayolan
and not ayolan
).
我想知道我是否应该使用PascalCasing
或camelCasing
用于我的模块名称,到目前为止我一直使用 PascalCasing,即:Ayolan.Engine.Rendering
而不是Ayolan.engine.rendering
(我为容器保留 PascalCasing,因为我希望全局对象名称是Ayolan
而不是ayolan
)。
I haven't found any headline on this, found that threadfrom 3 years ago but not really useful.
我没有找到任何关于此的标题,找到了3 年前的帖子,但不是很有用。
I'm wondering because I'm working with Java developpers and to them it makes more sense to use camelCasing
, but that's not what I've seen so far with TS.
我想知道,因为我正在与 Java 开发人员一起工作,对他们来说使用 更有意义camelCasing
,但这不是我迄今为止在 TS 上看到的。
采纳答案by Fenton
In TypeScript, we use the same standards as JavaScript, because we are working with many JavaScript libraries (and potentially being consumed by JavaScript code too).
在 TypeScript 中,我们使用与 JavaScript 相同的标准,因为我们正在使用许多 JavaScript 库(并且也可能被 JavaScript 代码使用)。
So we prefer PascalCase for modules and classes, with members being camelCase.
所以我们更喜欢 PascalCase 用于模块和类,成员是驼峰式的。
module ExampleModule {
export class ExampleClass {
public exampleProperty: string;
public exampleMethod() {
}
}
}
The only other style rule I can think of is that constants are ALL_UPPER.
我能想到的唯一其他样式规则是常量是 ALL_UPPER。
You will notice that this blends in nicely with the following code:
您会注意到这与以下代码很好地融合在一起:
Math.ceil(Math.PI);
Most importantly of all - keep consistent with the style you use as the style can imply meaning, so if you aren't consistent it will cause confusion.
最重要的是 - 与你使用的风格保持一致,因为风格可能意味着意义,所以如果你不一致,就会引起混乱。
回答by Paleo
Notice: Internal modules are namespaces. The next version of TypeScript focuses on external modules from ECMAScript 6, which are not namespaces.
注意:内部模块是命名空间。下一版本的 TypeScript侧重于来自 ECMAScript 6 的外部模块,它们不是命名空间。
Microsoft didn't publish a naming guideline for internal modules in TypeScript.
Microsoft 没有发布 TypeScript 内部模块的命名指南。
Arguments in favor of PascalCase
支持 PascalCase 的论据
An internal module is like a class with only static members:
内部模块就像一个只有静态成员的类:
module MyLib {
export function f1() {
}
export function f2() {
}
// ...
}
Languages that use pascal case for namespaces: C#, PHP.
Arguments in favor of camelCase
支持camelCase的论据
A name in camel case is reminiscent of the Google Style Guide for JSON:
骆驼大小写的名称让人想起JSON的Google 样式指南:
Property names must be camel-cased, ascii strings.
属性名称必须是驼峰式大小写的 ascii 字符串。
… but an internal module is not a JSON object.
… 但内部模块不是 JSON 对象。
Languages that use camel case for namespaces: ? (Not Java, which uses lowercase).
使用驼峰命名空间的语言: ? (不是 Java,它使用小写字母)。