javascript - There are some uncertain factors. Will everyone choose to save it as a variable? ~
世界只因有你
世界只因有你 2017-05-19 10:34:18
0
2
487

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... …

世界只因有你
世界只因有你

reply all(2)
刘奇
  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.

大家讲道理

Determine a constant const

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!