====================================
Browser runtime
- Open a HTML file with js modules directly from default browser will throw an error. You need to create a local server with VSCode.
- The script.js file has imported module should have an attribute
type= 'module'
when linking to HTML, the file has exported module doesn't need to be linked to HTML. - To export several functions,
export {func1, func2...}
or addexport
before declare the function - To import a function
func
and rename it tonewfunc
,import {func as newfunc} from 'path'
- To export a function as default, use
export {func as default}
or simplyexport default func
- To import a default value,
import importfunc from path
works the same withimport { default as importedResources } from 'path'
- object-destructuring in import. MDN Web Docs on object destructuring
const resources = {
func1,
func2
}
export default resources;
import resources from 'path'
const {func1, func2} = resources
====================================
Node.js runtime
-
module.exports.func1 = func1
to export function1. -
const func1 = require('./path')
to import values. For built in module, no need to provide the path. - object-destructuring in import:
module.exports = {
func1,
func2
}
import way 1:
const func = require('path');
const func1Value = func.func1;
const func2Value = func.func2;
import way 2:
const {func1, func2} = require('path')
Top comments (0)