Home > Software engineering >  Read and Write a global array (vec3) in multiple files
Read and Write a global array (vec3) in multiple files

Time:01-17

If I initialize an array in first.js like this:

var arr = [1,2,3];
export { arr };

Or like this:

var arr = new Array(1, 2, 3);
export { arr };

And try to use it in second.js like this:

import { arr } from "./first.js";

My array will undefined in second.js. :(

If I use the global variable window in first.js like this:

window.arr = new Array(1, 2, 3);

If I try to use it in second.js the array will undefined too:(

How to properly share an array between files?

CodePudding user response:

first.js

export const array = [1, 2, 3];

second.js

import { array } from './first.js';
console.log(array); // Output: [1, 2, 3]
  • Related