0

I have a bit of code like

var myClass = function(myArray) {
    this.arr = array;    
}
myClass.prototype.myMethod = function () {
     //random code
};

module.exports = myClass;

When I start node from the console, require this file with var a = require('./myClass.js') and try to instantiate my class with

myclass = new myClass([1,2,3]);

what I get is ReferenceError: myClass is not defined. I am building a very small and easy game, I wonder how I would go about testing it and playing around with it from the node console. Thanks.

2
  • 2
    Show your require statement Commented May 14, 2018 at 22:49
  • Show us how are you importing that module. require statement Commented May 14, 2018 at 23:00

1 Answer 1

3

When you do this:

 var a = require('./myClass.js') 

a becomes the thing you exported from 'myClass.js'. You would need to use it like:

var instance = new a([1, 2, 3])

You can do that, but it's probably easier to read and understand if you use a more descriptive variable name:

var myClass = require('./myClass.js')
// myClass is the function exported in `myClass.js`

var someInstance = new myClass([1,2,3]);

Also, you probably want to use myArray rather than array here:

var myClass = function(myArray) {
    this.arr = myArray // not array;    
}
Sign up to request clarification or add additional context in comments.

1 Comment

Sure, the array bit was just a random thing i filled in. The problem was the require statement, I think I need to read seriously about how js works.

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.