Naming convention for const object keys in ES6

前端 未结 4 1113
情话喂你
情话喂你 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:30

    Naming conventions are all over the place, I personally still haven't decided on my preference but to add to the discussion this is what Airbnb JavaScript Style Guide says (see the last examples):

    // 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'
    };
    

提交回复
热议问题