ES6 中 const 对象键的命名约定
Naming convention for const object keys in ES6
在 es6 的 const 对象中是否有推荐的键名命名约定?我一直没能找到说明它们应该是大写还是小写的资源。
const COLOR_CODES = {
BLUE: 1,
RED: 1
};
对
const COLOR_CODES = {
blue: 1,
red: 1
};
The examples 这篇 MDN 文章显示了两种样式,因此可能两者都可以接受。
根据 Google 这将全部大写。根据经验,大多数其他编程语言都是大写的,所以我建议使用大写。
使用 NAMES_LIKE_THIS
作为常数值。
使用@const
表示常量(不可覆盖)指针(变量或属性)。
Googlejavascript引导
https://google.github.io/styleguide/javascriptguide.xml
Google曾推荐如下:
const COLOR_CODES = {
BLUE: 1,
RED: 1
};
参见:https://google.github.io/styleguide/javascriptguide.xml#Constants
- 使用
NAMES_LIKE_THIS
作为常量 values.
- 使用
@const
表示常量(不可覆盖)指针(变量或属性)。
- 切勿使用
const
关键字,因为 Internet Explorer 不支持它。
但是,更新后的 style guidelines 有不同的建议。
注意:请注意,已接受的回复有一个 link 到过时的 Google 风格指南
这很好(字符串文字或整数文字):
const PI = 3.14;
const ADDRESS = '10.0.0.1';
但是...
const myObject = { key: 'value' };
const userSuppliedNumber = getInputNumber()
Google JavaScript Style Guide 说:
Declare all local variables with either const or let. Use const by
default, unless a variable needs to be reassigned. The var keyword
must not be used.
Every constant is a @const static property or a module-local const
declaration, but not all @const static properties and module-local
consts are constants. Before choosing constant case, consider whether
the field really feels like a deeply immutable constant. For example,
if any of that instance's observable state can change, it is almost
certainly not a constant. Merely intending to never mutate the object
is generally not enough.
...capital-named constants are only used as aliases for “hard-coded”
values.
命名约定到处都是,我个人还没有决定 my preference but to add to the discussion this is what Airbnb JavaScript Style Guide 说(见最后的例子):
// bad
const PRIVATE_VARIABLE = 'should not be unnecessarily uppercased within a file';
// bad
export const THING_TO_BE_CHANGED = 'should obviously not be uppercased';
// bad
export let REASSIGNABLE_VARIABLE = 'do not use let with uppercase variables';
// ---
// allowed but does not supply semantic value
export const apiKey = 'SOMEKEY';
// better in most cases
export const API_KEY = 'SOMEKEY';
// ---
// bad - unnecessarily uppercases key while adding no semantic value
export const MAPPING = {
KEY: 'value'
};
// good
export const MAPPING = {
key: 'value'
};
在 es6 的 const 对象中是否有推荐的键名命名约定?我一直没能找到说明它们应该是大写还是小写的资源。
const COLOR_CODES = {
BLUE: 1,
RED: 1
};
对
const COLOR_CODES = {
blue: 1,
red: 1
};
The examples 这篇 MDN 文章显示了两种样式,因此可能两者都可以接受。
根据 Google 这将全部大写。根据经验,大多数其他编程语言都是大写的,所以我建议使用大写。
使用 NAMES_LIKE_THIS
作为常数值。
使用@const
表示常量(不可覆盖)指针(变量或属性)。
Googlejavascript引导 https://google.github.io/styleguide/javascriptguide.xml
Google曾推荐如下:
const COLOR_CODES = {
BLUE: 1,
RED: 1
};
参见:https://google.github.io/styleguide/javascriptguide.xml#Constants
- 使用
NAMES_LIKE_THIS
作为常量 values. - 使用
@const
表示常量(不可覆盖)指针(变量或属性)。 - 切勿使用
const
关键字,因为 Internet Explorer 不支持它。
但是,更新后的 style guidelines 有不同的建议。
注意:请注意,已接受的回复有一个 link 到过时的 Google 风格指南
这很好(字符串文字或整数文字):
const PI = 3.14;
const ADDRESS = '10.0.0.1';
但是...
const myObject = { key: 'value' };
const userSuppliedNumber = getInputNumber()
Google JavaScript Style Guide 说:
Declare all local variables with either const or let. Use const by default, unless a variable needs to be reassigned. The var keyword must not be used.
Every constant is a @const static property or a module-local const declaration, but not all @const static properties and module-local consts are constants. Before choosing constant case, consider whether the field really feels like a deeply immutable constant. For example, if any of that instance's observable state can change, it is almost certainly not a constant. Merely intending to never mutate the object is generally not enough.
...capital-named constants are only used as aliases for “hard-coded” values.
命名约定到处都是,我个人还没有决定 my preference but to add to the discussion this is what Airbnb JavaScript Style Guide 说(见最后的例子):
// bad
const PRIVATE_VARIABLE = 'should not be unnecessarily uppercased within a file';
// bad
export const THING_TO_BE_CHANGED = 'should obviously not be uppercased';
// bad
export let REASSIGNABLE_VARIABLE = 'do not use let with uppercase variables';
// ---
// allowed but does not supply semantic value
export const apiKey = 'SOMEKEY';
// better in most cases
export const API_KEY = 'SOMEKEY';
// ---
// bad - unnecessarily uppercases key while adding no semantic value
export const MAPPING = {
KEY: 'value'
};
// good
export const MAPPING = {
key: 'value'
};