export
规定模块对外的接口
输出变量
写法1
~
export const name = 'fredZhao';
export const age = 23;
export const number = 233;
~
写法2
~
const name = 'fredZhao';
const age = 23;
const number = 233;
export {name,age,number}
~
将该模块的三个参数暴露给外部调用
输出函数
写法1
~
export function(x,y){
rturn x * y;
}
~
写法2
~~~
function fun1(){
return 2;
}
function fun2(){
return 2;
}
export {
fun1 as number1,
fun2 as number2,
funN as number3
}
// 使用as关键字给导出的函数重新命名。在重新命名下,同一个函数可以被输出两次
~~~
写法3
~
// 输出变量
// -- 1
var num = 1;
export {num};
// --2
export var foo = bar
setTimeout(()=> foo = 'num',2000);
~
~~ES6中输出的变量,需要与模块内部有关联,在两秒后foo会变成num~~
==需要注意的是,该方法不能出现在块级作用域中==
import
加载export
暴露在外的模块
导入模块
写法1
import {name,age,number} form './profile';
// 可以给导入的模块重新命名
import {name as userName} from './profile';
导入另一个文件暴露给外部的模块
写法2
~
import * as circle from './circle';
// 导入circle文件的所有模块
// 调用方法
circle.fun1;
circle.fun2;
~