After updating a Vue2 project to Vue3, I ran into an issue with Javascript. It seems that the language now prevents me from assigning a function to an object.
In the code below, I define a function "bar" within a loop. While I can successfully call the function within the loop as "bar(i)", and outside the loop as "ff('00')", I encounter an error when trying to assign the function to the api_calls object. This is puzzling to me and I'm not sure what mistake I might be making.
I also upgraded from node-v10.15.3-x64 to node-v18.16.0-x64.
export function generate_api_calls(app_name) {
let api_calls = {}
api_calls['cameraFeed'] = `http://${hostname}:${camera_port}${CAMERA_FEED_URL}`
let ff
let items = [1, 2, 3, 4]
for (let i in items) {
let name = items[i]
function bar(j) {
logger.info(`bar ${j}`)
};
bar(i)
ff = bar
api_calls[name] = bar;
logger.info(`generate_api_calls api ${name} ${JSON.stringify(api_calls)}`)
logger.info(`generate_api_calls api ${JSON.stringify(api_calls[name])}`)
}
ff('00');
logger.info(`generate_api_calls 5 ${JSON.stringify(api_calls)}`)
return api_calls
}
The output is:
2023-05-30_17:39:24.582-gui.vue.app-INFO: bar 0
2023-05-30_17:39:24.582-gui.vue.app-INFO: generate_api_calls api 1 {"cameraFeed":"http://localhost:5051/camera/feed"}
2023-05-30_17:39:24.583-gui.vue.app-INFO: generate_api_calls api undefined
2023-05-30_17:39:24.583-gui.vue.app-INFO: bar 1
2023-05-30_17:39:24.584-gui.vue.app-INFO: generate_api_calls api 2 {"cameraFeed":"http://localhost:5051/camera/feed"}
2023-05-30_17:39:24.584-gui.vue.app-INFO: generate_api_calls api undefined
2023-05-30_17:39:24.585-gui.vue.app-INFO: bar 2
2023-05-30_17:39:24.585-gui.vue.app-INFO: generate_api_calls api 3 {"cameraFeed":"http://localhost:5051/camera/feed"}
2023-05-30_17:39:24.586-gui.vue.app-INFO: generate_api_calls api undefined
2023-05-30_17:39:24.586-gui.vue.app-INFO: bar 3
2023-05-30_17:39:24.587-gui.vue.app-INFO: generate_api_calls api 4 {"cameraFeed":"http://localhost:5051/camera/feed"}
2023-05-30_17:39:24.587-gui.vue.app-INFO: generate_api_calls api undefined
2023-05-30_17:39:24.588-gui.vue.app-INFO: bar 00
2023-05-30_17:39:24.588-gui.vue.app-INFO: generate_api_calls 5 {"cameraFeed":"http://localhost:5051/camera/feed"}