0
//default.js
const item = require("./item.js");
var itemTest = new item.ItemTest("weapon",1,1,1);
console.log(itemTest.name);

//item.js

module.exports = class ItemTest {
    constructor(name, value, attack, defense) {
        this.name = name;
        this.value = value;
        this.attack = attack;
        this.defense = defense;
    }

}

I've also tried it with simply

 //item.js
    function ItemTest(name, value, attack, defense) {
        this.name = name;
        this.value = value;
        this.attack = attack;
        this.defense = defense;
    }

but that also returns "item.ItemTest is not a constructor". if that function is added into default.js then it works just fine, but I don't know how to make it pull the constructor object from the other file.

2
  • With regard to your first try, change it to var itemTest = new item("weapon",1,1,1); (You're exporting the class, so in default.js, item is the class; just imagine whatever you're setting to module.exports is what replaces require()) Commented Dec 25, 2017 at 21:04
  • Whatever you assign to module.exports IS what you get when you do require() of that module. So when you do const Item = require('./item.js');, then Item is your class that you exported so you'd just do new Item(). Commented Dec 25, 2017 at 21:22

2 Answers 2

1

I made few changes to your existing code by replacing these lines const item = require("./item.js"); and var itemTest = new item.ItemTest("weapon",1,1,1); with these const ItemTest = require("./item"); and var itemTest = new ItemTest("weapon", 1, 1, 1);

//default.js
const ItemTest = require("./item");
var itemTest = new ItemTest("weapon", 1, 1, 1);
console.log(itemTest.name);

//item.js
class ItemTest {
    constructor(name, value, attack, defense) {
        this.name = name;
        this.value = value;
        this.attack = attack;
        this.defense = defense;
    }

}

module.exports = ItemTest;

In the code above, I am exporting ItemTest so you have access to it when you use require(). On requiring the file, you get the class exported.

Sign up to request clarification or add additional context in comments.

1 Comment

This is correct, but please also explain what you changed and why.
0

In Item.js you need to change

class ItemTest {
    constructor(name, value, attack, defense) {
        this.name = name;
        this.value = value;
        this.attack = attack;
        this.defense = defense;
    }

}

class MyClass {}

// If you want to export more stuff, do
module.exports = {ItemTest: ItemTest, MyClass: MyClass};


// But if you have only ItemTest, then you can do
module.exports = ItemTest;

// This will change the main code to be like 
var ItemTest = require('./item');
var itemTest = new ItemTest();

4 Comments

mind explaining what module.exports = {ItemTest: ItemTest}; does? What's the first and the second ItemTest specificailly doing?
It will export json object with key ItemTest and its value is the class ItemTest. This needed since you are calling item.ItemTest after importing the file which require item to have json key ItemTest.
Much better: exports.ItemTest = ItemTest;.
The "much better" is exports = ItemTest; and var ItemTest = require('./item.js'); but I did export {ItemTest} in case he wants to export more stuff within the same file :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.