I would like to show you an alternative solution posted by @Andy Gaskell.
First, you need babel to be sure you can use ES6 in your browser. This is necessary so that your code still works, as some browsers (such as IE) do not support modern javascript functions (ES6 and higher), such as import / export and classes.
You can add the following script
`<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser.min.js"></script>`
before any other javascript files mentioned above.
Secondly, if you include your javascript classes in a series, the scope of these classes becomes global, even if they are in your physical js files.
I included the working example below, I modified it a bit to make it work in the code snippet. You want to replace the script with a script that contains your javascript file, as you did in your code.
<!DOCTYPE html> <html lang = "en"> <head> <meta charset = "UTF-8"> <title>Javascipt by Rasik Bihari Tiwari</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser.min.js"></script> <script type="text/javascript"> class Rectangle{ constructor(height, width) { this.height = height; this.width = width; } } class MyHiFiRectangle extends Rectangle { constructor(height, width) { super(height,width); this.foo= "bar"; } } var v = new MyHiFiRectangle(2,4); console.log(v.foo); </script> </head> <body > </body> </html>
UPDATED
OK. cool! Btw, if I bring all the class definitions to a script tag of my html page itself, then I donβt even need to refer to babel-core in the header. Why is this required?
This may be needed for browsers that do not support classes such as IE. But if the compatibility for older browsers does not meet your requirements, you do not need it.
... do I even need materials for export-import? What would be the value of exporting a module to native javascript when each class is more or less global?
Indeed, you will not need export-import elements, since your classes are global. You only use this if you want to use the module system. If you are not using import / export, your classes should be global and therefore should work. But in case this is not so. You will verify that it exists globally by binding it to the window object as follows:
window.myClass = class MyClass { }