Say I have a file1.js with an array.
var array = [data, more data, ...];
Is there any way to read that array from a different file? If not, what are the conventions for having a big array in a file?
Say I have a file1.js with an array.
var array = [data, more data, ...];
Is there any way to read that array from a different file? If not, what are the conventions for having a big array in a file?
Well several years ago the way to do so would be make it a global and have to worry about the order your scripts are loaded, which you can still do. However in this day and age, there are several other ways you can do. This is done through some sort of module concept. There's, requirejs, commonjs, browserify, nodejs's native module system and ES6 import/export.
So in most cases, what you would do is something like (keyword like because some do vary) this:
file 1
module.exports.bigArray = [data];
file 2
var bigArray = require('./file1').bigArray;
You can read it if file1.js is downloaded by the browser before your different file. and your variable array
is available.
<script type="text\javascript" src="file1.js"></script>
<script type="text\javascript" src="differentFile.js"></script>
If possible, please namespace them so that array
is not in the global scope.