I have my file structure like this:
classfile.js
class Car {
constructor(name, year) {
this.name = name;
this.year = year;
}
}
mainfile.js
import {Car} from './classfile.js'; // Added this
const myCar = new Car("Ford", 2014);
function myFunction() {
document.getElementById('demo').innerHTML = myCar.name + " " + myCar.year;
}
main.html
<!DOCTYPE html>
<html>
<head>
<script src="classfile.js"></script>
<script type="module" src="mainfile.js"></script>
</head>
<body>
<h2>JavaScript Class</h2>
<p>How to use a JavaScript Class.</p>
<p id="demo" onclick="myFunction()">Click Me</p>
</body>
</html>
I keep getting errors myFunction() is not defined.
How do I make functions in module type to execute?