ES6-模块

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012054869/article/details/82109897

1. 模块化的标准

(1)commonJS标准(node.js采用)

(2)AMD标准(require.js采用)

(3)ES6模块

2. 定义模块

export,可以export任意类型的数据

3.引入模块

import,可以起别名

index.html

<script type="module">
	//引入模块
	// import {lastName,firstName,getJson} from "./module01.js";

	//同样将lastName起个别名
	import {lastName,firstName as name,getJson} from "./module01.js";
	console.log(name);
	console.log(lastName);//Jack
	getJson();//getJson

	//第二种:js写法
	import {Point,demo,obj} from "./module02.js";
	demo();//demo
	console.log(obj);//第二种demo
	new Point();

	//第三种:在js中起别名
	import myobj from "./module03.js";
	console.log(myobj);//Object { name: "", age: "", getInfo: getInfo() }

</script>

 

module01.js

export var lastName = "Jack";
export var firstName = "Bob";
export function getJson(){
	console.log("getJson");

module02.js

class Point{
}
function demo(){
	console.log("第二种demo");
}
let obj = {name : "xiaohong"}
export {Point,demo,obj}

module03.js

//对象
let obj = {
	name: "",
	age: "",
	getInfo:function(){
	}
}
// export{obj as myobj};
// 这时html中:import {myobj} from "./module03.js";

//同样还可以这样写
export default obj;
// 这时html中:import myobj from "./module03.js";

猜你喜欢

转载自blog.csdn.net/u012054869/article/details/82109897