1.import,export——模块化
exp.js
export let a = 10; export let person = {name:'lily'} export function test(){console.log("hello");} export class Animal{ }
index.js
import {a} form './exp.js'; a = 100;//报错 a是常量不能改 console.log(a) //10 =========== import {a,person} form './exp.js'; // a = 100;//报错 a是常量不能改 //如果是基本类型,不能改,如果是对象如{a,person}就可以修改 person.name = 'mickey';//对象可以修改属性 console.log(a) //10 console.log(person.name) //mickey =========== import {a,person,Animal} form './exp.js'; let ani = new Animal(); console.log(ani)
导入方式比较
index.js
import {exp} from './exp.js' console.log(exp.a); console.log(exp.Animal); ============= 一次性导入某个文件的所有模块并重新命名 import * as exp from './exp.js'; console.log(exp.a); console.log(exp.Animal);
将所有要导入的元素放入default里面
// exp.js let a = 10; let person = {name:'tom'}; function test(){console.log('hello')}; class Animal{} export default{ a,person,test,Animal } //default只能使用一次 // index.js import Exp from './exp.js'; console.log(Exp.a); console.log(Exp.Animal);