小编今天在用Vue做项目的时候,发现组件中有import和export,刚好今天看到相关的语法介绍和一些实例,下面小编就和大家一起进步。对于模块化规范,在es6出现之前,有以下三种规范,分别是Common.js(Node.js)、AMD(require.js)、CMD(sea.js)。大家还可以关注我的微信公众号,蜗牛全栈。

一、基本用法

// module.js
export const a = 9
export const b = "abc"
export const sum = (x,y) => x+y
const obj = {
name:"lilei"
}
export {obj} // 导出对象的时候需要添加花括号 // index.js
import {a,b,sum,obj} from "./module.js" // 必须保证引入的名和export名称完全一致
console.log(a,b) // 5 abc
console.log(sum(2,5)) // 7
console.log(obj) // {name:"lilei"}

二、import 别名

// module.js
export const a = 9
// index.js
import { a as aa } from "./module.js" // 通过as关键字重命名,在使用的时候,使用重命名后的名字即可
console.log(a) // undefind
console.log(aa) // 9

三、export default:只能export default一个数据,如果里面有多个数据,可以通过对象实现。

// module.js
// export defalut const a = 9 报错
const a = 9
const b = "abc"
export defalut a
// index.js
import a from "./module.js" // 导入默认导出的量,不需要用花括号

四、混合导出

// module.js
export const a = 5
export default b = "abc"
// index.js
import {a},b from "./module.js" // 导入不同形式导出的数据

五、导出多个内容

// module.js
class People{
constructor(name){
this.name = name
}
showName(){
console.log("我的名字是:"+this.name)
}
}
const a = 9
const b = "abc"
const sum = (x,y) => x+y
const obj = {
name:"lilei"
}
export {
a,b,sum,obj,People
}
// index.js
import {a,b,sum,obj,People} from "./module.js"
let p = People("lilei")
p.showName() // 我的名字:lilei
// module.js
class People{
constructor(name){
this.name = name
}
showName(){
console.log("我的名字是:"+this.name)
}
}
const a = 9
const b = "abc"
const sum = (x,y) => x+y
const obj = {
name:"lilei"
}
export default {
a,b,sum,obj,People
}
// index.js
import * as aa from "./module.js"
console.log(aa.default.a) // 9

六、创建Interator

function makeInterator(arr){
let nextIndex = 0;
return {
next(){
return nextIndex<arr.length? {
value:arr[nextIndex++],
done:false
}:{
value:undefined,
done:true
}
}
}
} let it = makeInterator(['a','b','c'])
it.next() // {value:"a",done:false}
it.next() // {value:"b",done:false}
it.next() // {value:"c",done:false}
it.next() // {value:undefind,done:true}

七、处理不可遍历对象可以使用for...of...函数。个人感觉类似python中的魔法函数

let course = {
allCourse:{
science:["math","physics","chemistry"],
arts:["geography","history"]
}
} // for...of...
// for(let c of course){
// console.log(c) // 报错:course is not iterable
// } let arr = ["a","b","c"]
console.log(arr) // Symbol(Symbol.iterator) prototype中含有这个属性的,是可遍历的
let it = arr[Symbol.iterator]() // 固定语法
console.log(it.next()) // {value:"a",done:false}
console.log(it.next()) // {value:"b",done:false}
console.log(it.next()) // {value:"c",done:false}
console.log(it.next()) // {value:undefind,done:true}

八、通过处理Symbol.iterator属性来遍历。

let courses = {
allCourse:{
science:["math","physics","chemistry"],
arts:["geography","history"]
}
} courses[Symbol.iterator] = function(){
let allCourse = this.allCourse
let keys = Reflect.ownKeys(allCourse)
let values = []
return { next(){
if(!values.length){
if(keys.length){
values = allCourse[keys[0]]
keys.shift()
}
}
return {
done: !values.length,
value: values.shift()
}
}
}
} for(let c of courses){
console.log(c) // math physics chemistry geography history
}

九、使用generator

let courses = {
allCourse:{
science:["math","physics","chemistry"],
arts:["geography","history"]
}
} courses[Symbol.iterator] = function* (){
let allCourse = this.allCourse
let keys = Reflect.ownKeys(allCourse)
let values = []
while(1){
if(!values.length){
if(keys.length){
values = allCourse[key[0]]
keys.shift()
yield values.shift()
}else{
return false
}
}else{
yield values.shift()
}
}
} for(let c of courses){
console.log(c) // math physics chemistry geography history
}

十、原生具备Interator接口的数据结构,通过for...of...直接遍历

  • Array
  • Map
  • Set
  • String
  • TypedArray
  • 函数的arguments对象
  • NodeList对象

ES6中的Module与Interator的更多相关文章

  1. Nodejs与ES6系列4:ES6中的类

    ES6中的类 4.1.class基本语法 在之前的javascript语法中是不存在class这样的概念,如果要通过构造函数生成一个新对象代码 function Shape(width,height) ...

  2. ES6中的Symbol类型

    前面的话 ES5中包含5种原始类型:字符串.数字.布尔值.null和undefined.ES6引入了第6种原始类型——Symbol ES5的对象属性名都是字符串,很容易造成属性名冲突.比如,使用了一个 ...

  3. ES6中的模块

    前面的话 JS用"共享一切"的方法加载代码,这是该语言中最容出错且容易令人感到困惑的地方.其他语言使用诸如包这样的概念来定义代码作用域,但在ES6以前,在应用程序的每一个JS中定义 ...

  4. ES6中export , export default , import模块系统总结

    最近在学习使用Webpack3的时候发现,它已经可以在不使用babel的情况下使用ES6的模块加载功能了. 说到ES6的模块加载功能,我们先复习一下CommonJS规范吧: 一  . CommonJS ...

  5. es6中的模块化

    在之前的javascript中是没有模块化概念的.如果要进行模块化操作,需要引入第三方的类库.随着技术的发展,前后端分离,前端的业务变的越来越复杂化.直至ES6带来了模块化,才让javascript第 ...

  6. CommonJS 规范中的 module、module.exports 区别

    CommonJS 规范中的 module.module.exports 区别 CommonJS规范规定,每个模块内部,module变量代表当前模块.这个变量是一个对象,它的exports属性(即mod ...

  7. ES6中关于数据类型的拓展:Symbol类型

    ES5中包含5种原始类型:字符串.数值.布尔值.null.undefined.ES6引入了第6种原始类型——Symbol. ES5的对象属性名都是字符串,很容易造成属性名冲突.比如,使用了一个他人提供 ...

  8. ES6中比较实用的几个特性

    1.Default Parameters(默认参数) in ES6 es6之前,定义默认参数的方法是在一个方法内部定义 var link = function (height, color, url) ...

  9. ES6中Class与export简单用法

    一.Class ES6中的Class用法类似Java的Class用法,但class的本质是js一个function //定义类 class Person { //定义构造方法 constructor( ...

随机推荐

  1. (一)RabbitMQ安装与基本配置

    [博主使用的环境是阿里云ecs服务器,操作系统为centos] 安装erlang环境 RabbitMQ底层是Erlang语言,因此要先安装erlang环境,就像你要运行Java程序就必须先安装JRE/ ...

  2. SSM久别遇新坑

    SSM久别遇新坑 久别个锤子,也就几天没看,改bug改到怀疑人生 maven的父子模块问题 众所周知,用maven建立一个空的模块,在它之下,将原本的各层次结构分别新建为一个子模块,就能够将各业务进行 ...

  3. UI设计师、平面设计师常用的网站大全,初学者必备,大家都在用!

    UI设计师.平面设计师常用的网站大全,初学者必备,大家都在用! 国外的花瓣--Pinterest • The world's catalog of ideas 颜格视觉--app界面设计大全--电商. ...

  4. 『动善时』JMeter基础 — 14、使用JMeter发送Post请求

    目录 1.Post请求参数类型说明 2.用于演示的项目说明 3.发送Post请求示例 (1)测试计划内包含的元件 (2)请求参数类型为x-www-form-urlencoded 4.请求参数form- ...

  5. RabbitMQ高级特性

    消息的可靠投递 在使用Rabbitmq的时候,作为消息发送方希望杜绝任何消息丢失或者投递失败场景.Rabbitmq为我们提供了两种方式用来控制消息的投递可靠性模式 confirm确认模式 return ...

  6. 【转载】Linux踢出其他正在SSH登陆用户

    Linux踢出其他正在SSH登陆用户     在一些生产平台或者做安全审计的时候往往看到一大堆的用户SSH连接到同一台服务器,或者连接后没有正常关闭进程还驻留在系统内.限制SSH连接数与手动断开空闲连 ...

  7. 利用rsync备份生产应用(一)

    rsync简单介绍 Rsync(remote synchronize)是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件.Rsync使用所谓的"Rsync算法"来 ...

  8. VMware(Caps Lock键)切换大小写作用失效的Bug的解决办法

    前言 第一种情况是:进入VMware虚拟机的时候,即使按了Capslock键开启大写,灯虽然亮了,但是,打出来的字母还是小写,没有有任何的效果,根本不能转换成大写. 只有按Shift+字母才能输入大写 ...

  9. 855 gpu强 730 3倍

    骁龙730G的GPU规模只有骁龙835的GPU规模的一半,Adreno 618是128 ALUs,而Adreno 540是256 ALUs. 根据GFXBench的数据,对GPU负载比较大的曼哈顿3. ...

  10. Go语言安装配置

    一.Go语言下载 官方下载地址:https://golang.google.cn/dl/ 选择自己需要的版本下载即可. 二.Go语言安装 下载完成之后,双击go1.16.4.windows-amd64 ...