Skip to content Skip to sidebar Skip to footer

How To Define A Structure And Create Multiple Instances Of It In Different Javascript Files

I have two files within the same project, one.js and two.js. File one.js var fruit = { name, color, } File two.js // Here I receive a JSON string with list of fruit objects.

Solution 1:

I'm not really sure why would you want to create the instances like that, but there might be some implications.

You can use Class property of ES6;

one.js

class Fruit {
  constructor(name, color) {
    this.name = color;
    this.color = color;
  }
}

two.js

var fruit_1 = new Fruit("apple", "red");
var fruit_2 = new Fruit(data_name, data_fruit);

Read On: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Classes


Also really advise you to use modules and es6 preset instead including javascript files one by one to your HTML file.

See: https://babeljs.io/


Post a Comment for "How To Define A Structure And Create Multiple Instances Of It In Different Javascript Files"