I'm new to this stuff but i'm trying to create a new instance of a class in javascript that class I also want to initialise using input, like a function.
So far I have:
/Class1.js
/folder/Class2.js
Class2.js:
function Class2(n) {
this.number = n;
}
Class1.js:
var instance = new Class2(1);
That didn't work so I tried requiring the class:
require('./folder/Class2.js');
or
var newClass = new require('./folder/Class2.js');
var instance = new newClass(1);
Neither worked and I get the error:
Class2 is not defined
No online examples such as this haven't gone into any detail why it doesn't work just the same non-working stuff.
classis a reserved identifier, so you can't have a variable namedclass. In fact, in ECMAScript 6 it is used to create classes.