I am defining pageId = 3
in JavaScript in one file and socket = io()
in another file and I want to use these variables in other files.
I guess I should use modules in ES6, but I am not sure how to do this.
Right now it does work since I am defining the two variables in files that are loaded before the files where I actually use the variables, but it seems like bad code, and my code IDE editor gives me a warning that the variables aren't defined, because it cannot see it in the current file.
I have read a few tutorials, but I still can't see how to define a variable in one file and export it to other files.
Would it just be
const pageId = 3;
const socket = io(); // which is defined in file 'socket.io-1.4.5.js' which is loaded before this file
module.exports = { pageId, socket };
and then import it in the other files with
const pageId = require('pageId');
const socket = require('socket');
socket.on('connect', ...);
(...)
without even loading these files anywhere?