I am pretty new to this and I am trying to understand classes in javascript. So I followed a tutorial online and everything seemed to work fine and when I tried to do the same like it was in the video I now get an error saying "Uncaught ReferenceError: MyClassName is not defined"
I have 2 files:
- app.js
- class.js
In class.js
class MyClass {
constructor(city, state){
this.city = city;
this.state = state;
}
message(){
console.log('Hey!');
}
}
in app.js
const myClass = new MyClass();
const btn = document.getElementById('submit');
btn.addEventListener('click', myClass.message());
The error I get is "Uncaught ReferenceError: MyClass is not defined".
My question is how do I instantiate a class from another file? I know I am doing something wrong but I have no idea what it is.
Update: My HTML doc goes like this.
<body>
<div class="form-group">
<button id="submit" name="submit">Submit</button>
</div>
<script src="app.js"></script>
<script src="class.js"></script>
</body>
</html>
Please help, thank you for your time.