Naming convention for const object keys in ES6

前端 未结 4 1059
情话喂你
情话喂你 2021-01-31 08:03

Is there a recommended naming convention for key names within a const object in es6? I haven\'t been able to find a resource which states if they should be uppercase or lowercas

4条回答
  •  花落未央
    2021-01-31 08:42

    NOTE: be aware that the accepted response has a link to an obsolete Google style guide

    This is nice (string literals or integer literals):

    const PI = 3.14;
    const ADDRESS = '10.0.0.1';
    

    but...

    const myObject = { key: 'value' };
    const userSuppliedNumber = getInputNumber()
    

    Google JavaScript Style Guide says:

    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.

    JavaScript.info says:

    ...capital-named constants are only used as aliases for “hard-coded” values.

提交回复
热议问题