admin管理员组文章数量:1431712
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.
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.
Share Improve this question edited Nov 1, 2020 at 1:15 KodingRookie asked Nov 1, 2020 at 1:10 KodingRookieKodingRookie 631 silver badge5 bronze badges 3- make sure class.js is loaded before app.js – Jaromanda X Commented Nov 1, 2020 at 1:11
- How are these files included in your HTML file? – Randy Casburn Commented Nov 1, 2020 at 1:11
- Wow, I feel like an idiot. You are guys were right the class.js didn't load before the app.js.. hence the error. Like you mentioned I made sure the class.js loaded before app.js and it works fine. – KodingRookie Commented Nov 1, 2020 at 1:16
2 Answers
Reset to default 1I believe it's giving you that error because you imported the app.js file before class.js file. Try importing class.js first and then it should work. example:
<body>
<div class="form-group">
<button id="submit" name="submit">Submit</button>
</div>
<script src="class.js"></script>
<script src="app.js"></script>
</body>
</html>
Since you linked the app.js is linked before the class.js, it is loaded first, so you can't access MyClass. Switch the lines where you link app.js and class.js, and it should run with no errors.
<!DOCTYPE html>
<html lang="en">
<body>
<div class="form-group">
<button id="submit" name="submit">
Submit
</button>
</div>
</body>
<script type="text/javascript" src="class.js"></script>
<script type="text/javascript" src="app.js"></script>
</html>
本文标签: javascriptHow to call a class from anotherjs fileStack Overflow
版权声明:本文标题:javascript - How to call a class from another ,js file? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745592427a2665279.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论