Amy Teresa Hyland Amy Teresa Hyland - 1 year ago 77
Javascript Question

Data Objects & classes

Hi I am just wondering I need to create multiple data object variables on lots of different JavaScript file throughout my project that all have the same keys. These object will be used as a data source for a package I have to use. for example

V1: 0,
V2: 0,
V3: 0.

I was thinking of using JavaScript classes but I dont know if that is the correct way to do so. Something like

var i = new iVar();

So I created a class like so

class outputData {
this.O0 = 0;
this.O1 = 0;
this.O2 = 0;
this.O3 = 0;
this.O4 = 0;
this.O5 = 0;
this.O6 = 0;
this.O7 = 0;
this.O8 = 0;
this.O9 = 0;
this.O10 = 0;
this.O11 = 0;
this.O12 = 0;

I then imported it into the file I wanted to use it on lie so.

import '../OutPutDataClass.js';

var openingDeb = new outputData();

But i get a

Uncaught ReferenceError: outputData is not defined(…)

Not to sure why. The reason I wanted the objects initialized with data is so I can use a loop later on to iterate trough the object to call a function to set the correct values using a key and a reactive tracker. I am not sure if this is the correct way to go about doing this but I am still learning so if someone can educate me on this would be great. I have done some research on JavaScript classes but all the example create the class locally on the same file the call it from which is not what i need.

Thanks very much

Answer Source

The issue isn't with your class definition (as far as I can tell, it's fine!) - it's with your understanding of how the ES2015 import syntax works. There's no 'global scope' so to speak - if you want to export/import something, you have to be explicit about it, like so:

File 1:

export default class OutputData {

File 2:

import OutputData from '../OutPutDataClass.js';

var openingDeb = new OutputData();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download