TJS13
TJS13

Reputation: 56

How to access an array stored in a different js file

I'm trying to create my own lorem ipsum app and I want to keep my code clean by storing my word bank in other files. How can I access an array stored in a different JS file? For example, instead of hardcoding harry = ["", "", ""], I want to store that data in a different file and just call that file into the array.

// generator.js
function GenerateNewText(){
this.sentences = [
    harry = [
        "I am the chosen one!",
        "Boy wizard",
        "Quidditch seeker"
    ron = [
       "I am a Weasley",
       "Gryffindor",
       "Quidditch keeper"
    ]
  ]
}

GenerateNewText.prototype.getRandomSentence = function() {
   let randomSentence = this.sentences[0][Math.floor(Math.random() * this.sentences.length)]
   return randomSentence;
}

Currently, I have a harryText.js which contains

// harryText.js
harryText = [
  "I am the chosen one",
  "I am a Gryffindor",
  "I am a boy"
]

module.exports = harryText;

but doing this in my generator.js shows harryText is not defined

function GenerateNewText(){
this.sentences = [
    harryText,  <---- error here
    ron = [
       "I am a Weasley",
       "Gryffindor",
       "Quidditch keeper"
    ]
  ]
}

I tried requiring it like so const harryText = require("./harryText.js") and the problem persists. I'm guessing a scope issue?

I tried installing ejs and changing harryText.ejs and including it like <%= include harryText %> in the generator array and that's invalid code.

Is calling an array from another file and storing it within another array even possible? Does anyone know a solution to this?

And yes, I know a Harry Potter Ipsum already exists. This is just dummy text.

Upvotes: 2

Views: 6420

Answers (3)

TJS13
TJS13

Reputation: 56

UPDATE 2/20/19 16:15 I was getting the harryText is not defined error because I kept foolishly defining the variable in the wrong place! First I defined it outside of the function GenerateNewText, then I tried defining it inside the this.sentence. Instead, I should have been defining it inside the function GenerateNewText and outside this.sentence, like so

function GenerateNewText(){
  const harryText = require("./harryText");
  const ronText = require("./ronText");
  this.sentences = [
    harryText,
    ronText
  ]
}

Upvotes: 0

kanhai shah
kanhai shah

Reputation: 1333

If you are using ES6 you can do following.

//file1.js
  export const harryText = [
     "I am the chosen one",
     "I am a Gryffindor",
     "I am a boy"
  ]

//file2.js
  import {harryText} from './file1.js';
  // use variable here 

Upvotes: 1

powerc9000
powerc9000

Reputation: 2103

Javascript files are isolated from each other. To share common code you always need require and module.exports

So you are doing the right thing with module.exports = harryText

You then need to require that file in generator.js

const harryText = require("./harryText");
function GenerateNewText(){
this.sentences = [
    harryText,
    ron = [
       "I am a Weasley",
       "Gryffindor",
       "Quidditch keeper"
    ]
  ]
}

Upvotes: 1

Related Questions