My goal is to improve the organization of my code by creating a config file where I can use the same method to set values. This is just a demonstration, so please keep that in mind. I'm wondering if there's a way to call a function based on whether the car
or bike
key of the config
object is selected. If there is, could you provide some guidance on how to do this?
I encountered an error stating that setYear
is undefined. My objective is to set the year of the car object using the config object and access the setYear
function through it.
const buildConfig = () => {
const car = {}
car.label = config['car'].label;
car.year = config['car'].setYear('2020')
console.log('car details', car);
}
const config = {
'car': {
label: 'Sedan',
setYear: setYear
},
'bike': {
label: 'Sedan',
setYear: setYear
}
}
const setYear = (year) => {
car.year = year;
}
buildConfig();