小编今天在用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. utf8字符集下的比较规则

    前言: 在MySQL中,比较常用的字符集是utf8和utf8mb4.这两个字符集是类似的,utf8是utf8mb3的别名,所以之后在MySQL中提到utf8就意味着使用1~3个字节来表示一个字符,如果 ...

  2. 在Linux系统中部署NodeJS项目

    在Linux系统中部署NodeJS项目 安装NodeJS 首先进入 Node 官网,下载对应的 Node包 下载下来后是一个后缀为 xz 的压缩包,我们把这个包上传到 Linux 系统中的 /usr/ ...

  3. 10.Debug

    1.Debug模式 1.1 什么是Debug模式 是供程序员使用的程序调试工具,它可以用于查看程序的执行流程,也可以用于追踪程序执行过程来调试程序. 1.2 Debug介绍与操作流程 Debug调式, ...

  4. [Python] 微信公众号开发 Python3

    搭建服务 开通一个阿里云ecs,安装python3及需要的包(参考下方官方文档) 将py文件保存在ecs上,运行 在本地访问阿里云的IP地址 能完成这步说明网络没问题 server.py 1 # -* ...

  5. Python 送你一棵圣诞树

    Python 送你一棵圣诞树 2019-01-02阅读 8800   今天是圣诞节,先祝大家圣诞快乐!??? 有人要说了,圣诞节是耶稣诞生的日子,我又不信基督教,有啥好庆祝的.这你就有所不知了,Pyt ...

  6. python类内部方法__setattr__ __getattr_ __delattr__ hasattr __getattribute__ __getitem__(),__setitem__(), __delitem__()

    主要讲类的内部方法 __setattr__  __getattr_  __delattr__  hasattr  __getattribute__  __getitem__(),__setitem__ ...

  7. ltp

    1.查找文件 find / -name 'filename'   1 2.查找目录 find / -name 'path' -type d 1 3.查找内容 # find .| xargs grep ...

  8. Python实现TCP通讯

    Environment Client:Windows Server:KaLi Linux(VM_virtul) Network:Same LAN Client #!/usr/bin/python3 # ...

  9. DOCKER学习_012:Dockerfile配置指令详解

    1 Dockerfile结构 基础镜像信息 镜像操作指令 容器启动时执行指令 2 FROM 指定基础镜像,用于继承其他镜像使用的 FROM ubuntu:14.06 FROM centos FROM ...

  10. SpringCloud专题之开篇及Eureka

    声明: 本专题部分理论来自翟永超老师的<Spring Cloud微服务实战>.建议大家看原书. 开篇 微服务简单来说是系统架构上的一种设计风格,他的主旨是将一个原本独立且庞大的系统按照不同 ...