导出
let cat = { name: "小花", age: 2, cry(){ return "喵喵喵"; } } let dog = { name: "小黑", age: 1, cry(){ return "汪汪汪" } } let name = "测试文本" function sum(a,b) { return parseInt(a) + parseInt(b); } //同下:简写 exports = { cat, dog, name, sum, } //同下:简写 // module.exports = { // cat, // dog, // name, // sum, // } // 同上,原生 // module.exports = { // cat:cat, // dog:dog, // myName:name // }
导入
//可以导出全部 const m = require("./exportEs51") //可以导出部分 const {sum} = require("./exportEs51") //使用 function use(){ console.log(m.sum(10,20)) console.log(sum(20,20)); }
导出1
let cat = { name: "小花", age: 2, cry(){ return "喵喵喵"; } } let dog = { name: "小黑", age: 1, cry(){ return "汪汪汪" } } export {cat, dog}
导出2
export let cat = { name: "小花", age: 2, cry(){ return "喵喵喵"; } } export let dog = { name: "小黑", age: 1, cry(){ return "汪汪汪" } }
导入1、2
//可以导出全部 import {cat, dog} from "./exportEs61.js" //可以导出部分 import {cat} from "./exportEs61.js" function use(){ console.log(cat.cry()) console.log(dog.cry()) }
导出3
export default { sum(a,b){ return parseInt(a) + parseInt(b); }, cat:{ name: "小花", age: 2, cry(){ return "喵喵喵"; } }, dog:{ name: "小黑", age: 1, cry(){ return "汪汪汪" } } }
导入3
//导入名称可自定义,避免了重名问题 import m from "./exportEs61.js" console.log(m.cat)