Carol.Kar
Carol.Kar

Reputation: 5345

Call class directly and not via instance variable

I have created the following two classes in my classes.js:

class Person {
    constructor(firstName, lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    display() {
        console.log(this.firstName + " " + this.lastName);
    }
}

module.exports = {
    Person
};

As you can see I am exporting the two classes via module.exports.

Person = require('./classes.js');

const someone1 = new Person("First name", "Last name"); // <-- does NOT work

const someone = new Person.Person("First name", "Last name"); // does work
someone.display();

However, when calling the classes I get an error, when calling the class directly.

Any suggestions how to call the class directly?

I appreciate your replies!

Upvotes: 1

Views: 128

Answers (4)

1565986223
1565986223

Reputation: 6718

In this case you're exporting an object containing Person class

module.exports = {
    Person
};

So when you import like Person = require('./classes.js')

You're actually importing the exported object. So Person after you've imported is similar to

Person = {
  Person
};

You can either change the export by assigning the desired export object to module.exports like:

module.exports = Person

or change your import to use destructuring assignment to import like (notice the curly braces):

const { Person } = require('./classes.js');

Upvotes: 1

Iurii Tkachenko
Iurii Tkachenko

Reputation: 3189

In classes.js you are exporting Person as an object property. So to make it work as you expect you can either do Destructuring Assignment

const { Person } = require('./classes')

Or just require class directly from classes.js file.

const Person = require('./classes').Person

Alternatively, and it's a better option, in my opinion, you can separate Person to its own file and export it by default.

File ./Person.js

class Person {
    constructor(firstName, lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    display() {
        console.log(this.firstName + " " + this.lastName);
    }
}

module.exports = Person

And then in your main file just do:

const Person = require('./Person');

Upvotes: 1

alizahid
alizahid

Reputation: 969

If you want all your classes in one file called classes.js, then this should work;

// classes.js

class Person {
  // ...
}

class Vehicle {
  // ...
}

module.exports = {
  Person,
  Vehicle
}
// some-other-file.js

const { Person } = require('../classes')

const person = new Person('First', 'Last')

Although to keep things easy to understand, my recommendation would be to split your class into multiple files and then export each class from its file directly;

class Person {
  // ...
}

module.exports = Person
const Person = require('../classes/person')

// do something

Upvotes: 3

mbojko
mbojko

Reputation: 14679

If

module.exports = {
    Person
};

therefore

Person = require('./classes.js').Person;

Alternatively, you can

module.exports = Person;

and

Person = require('./classes.js');

Upvotes: 1

Related Questions