How to create a javascript module
井民全, Jing, mqjing@gmail.com
Key
Usage
File: index.js
File: index.html
Module
File: ./modules/my_module.js
How to create a javascript module
井民全, Jing, mqjing@gmail.com
# Usage File: index.html
File: index.js
// Module File: ./modules/my-module.js
|
File: index.js
import { myClass, myFunAbc } from './modules/my_module.js' myFunAbc(); var obj = new myClass(); obj.print(); |
File: index.html
<!doctype html> <html lang="en"> <head> <link rel="icon" type="image/x-icon" href="/images/favicon.ico"> </head> <body> <h1>Test</h1> <!-- using module syntax to identify the source javascript is a ECMAScript that support the import keyword--> <script type="module" src="index.js"> </script> </body> </html> |
File: ./modules/my_module.js
'use strict'; class myClass { constructor() { console.log("my_module::myClass::constructor"); } print() { console.log("my_module::myClass::print"); } } function myFunAbc() { console.log("my_module::myFunAbc()"); } export {myClass, myFunAbc} |