search

Home  >  Q&A  >  body text

javascript - There are some uncertain factors. Will everyone choose to save it as a variable? ~

For example, if you need to use an attribute of an object returned by an external method (assuming that this attribute is not of Object type and may be updated and cannot be saved as a variable...), will you store the key of this attribute? In case the attribute name is changed, just change the amount directly~

Also, when I wrote it, I defined a global object with an attribute named id. So if I want to add a value to the object during program iteration in the future, what should I do if there is a key name that is more consistent with the id key name and I want to change it... …

世界只因有你世界只因有你2751 days ago505

reply all(2)I'll reply

  • 怪我咯

    怪我咯2017-05-19 10:36:18

    1. In fact, in most cases, this is used directly. Sometimes you have to trust the value of the external method.

    2. This must be defined the first time. After all, idthis kind of name cannot be changed just by changing it.

    If the subject is worried, you can define a config file globally.

    // config.js
    
    export default {
        name: 'key_name',
        id: 'key_id',
        apiKey: 'api_return_key'
    }

    Where you want to define:

    import keys from '/config'
    const obj = {
        [keys.name]: 'hello world',
        [keys.id]: 1,
    }
    
    export default obj

    Wherever you want to quote, you have to take out the key stored in the global config:

    import keys from '/config'
    import obj from './obj'
    
    obj[keys.name] // hello world
    obj[keys.id] // 1
    
    something[keys.apiKey] // 拿到其他不确定是否会变的key名,只需要改config配置
    

    It’s just that it’s more troublesome, but it can still be implemented. At that time, you only need to change the name corresponding to each key in the config file object to achieve uniqueness.

    reply
    0
  • 大家讲道理

    大家讲道理2017-05-19 10:36:18

    Determine a constant const

    reply
    0
  • Cancelreply