es6模块的开发 ES6规范 基本使用

本文详细介绍了ES6中export和import命令的用法,涵盖三种导入方式:分别暴露、统一暴露和默认暴露。通过实例演示如何在模块间导入并使用函数、对象和变量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

基本用法

  • es6 中新增了两个命令 export 和 import ,
  • export 命令用于规定模块的对外接口,
  • import 命令用于输入其他模块提供的功能。

import三种方式:

  • 1、暴露模块 分别暴露
  • 2、暴露模块 统一暴露
  • 3、暴露模块 默认暴露 可以暴露 任意数据类型; 暴露的什么数据 接收到的就是什么数据

第一种方式:module1.js

// 暴露模块  分别暴露
 
export function foo(){
   console.log('foo() module1') 
}
 
export function bar(){
    console.log('bar() module1')
}
 
export let arr = [1,2,3,4,5,6]

第二种方式:module2.js

//暴露模块  统一暴露
function fun(){
    console.log('fun() module1')
}
function fun2 (){
    console.log('fun() module2')
}
 
export{ fun, fun2}

第三种方式:module3.js

//暴露模块  默认暴露 可以暴露 任意数据类型; 暴露的什么数据 接收到的就是什么数据
//export default value
 
//1暴露的方法 一个脚本里面有一个   export default
export default ()=>{
    console.log('默认暴露 写什么暴露什么')
 
}
 
//2暴露对象
export default{
    msg:'我是谁我在哪',
    foo(){
 
    }
}

在 Mian.js 中的使用

//引入其他模块
import {foo,bar} from './module1'
import {fun,fun2} from './module2'
 
import module3 from './module3'
 
console.log(foo)
console.log('>>>>>>>>>>>')
console.log(fun)
 
//当为方法时
module3()
 
 
//当为对象时
module3.foo()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值