<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>模块-1</title>
<!-- <script src="1.js"></script>
<script src="2.js"></script> -->
</head>
<body>
<script type="module">
// 模块: 就是一个外部的js
// 传统js,引入外部js,共享一个全局空间
// 模块有自己的独立作用域
// 导入模块
// import 成员列表 from 模块路径
import {email} from './1.js';
import {email,hello} from './1.js';
// 默认导出的成员不能放在大括号中,只能放前面
import hello,{email} from './1.js';
import { email, hello, user, Demo } from './1.js';
//{ email, hello, user, Demo } 给它找一个爸爸,全部挂上去
// 命名空间,就是所有导出成员组的别名
import * as phpcn from './1.js';
// console.log(email);
// console.log(hello(email));
// console.log(user);
// console.log(new Demo().show());
// 如果使用命名空间导出,在访问模块成员,必须带前命名空间前缀
// 当前空间前缀: phpcn
console.log(phpcn);
console.log(phpcn.email);
console.log(phpcn.hello(phpcn.email));
console.log(phpcn.user);
console.log((new phpcn.Demo()).show());
// 导入时也可以设置别名
import {email as myemail} from './2.js';
console.log(myemail);
// 导入成员的别名,可以在导出时设置,也可以在导入时设置
</script>
</body>
</html>
// 模块: 所有成员,默认都是私有的
let email = '123456@php.cn';
function hello(email) {
return 'My email is : ' + email;
}
let user = { x: 1, y: 2 };
class Demo {
show() {
return 'Hello php.cn';
}
}
// export { email, hello };
// hello 是默认导出成员,一个大括号只能有一个默认导出成员
// export { email, hello as default };
export { email, hello, user, Demo };