小编今天在用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. ElasticSearch第三弹之存储原理

    我们上文中介绍的ES内部索引的写处理流程是在ES的内存中执行的,而数据被分配到特定的主.副分片上之后,最终是存储到磁盘上的,这样在断电的时候就不会丢失数据.具体的存储路径可在配置文件 ../confi ...

  2. JavaScrip条件表达式优化

    目录 1,前言 2,多条件if语句优化 3,参数默认值 4,Switch语句优化 1,前言 今早看了一篇文章<JavaScrip实现:如何写出漂亮的条件表达式>,原创于:华为云开发者社区, ...

  3. Canal详细入门实战(使用总结)

    Canal介绍 Canal简介 canal [kə'næl],译意为水道/管道/沟渠,主要用途是基于 MySQL 数据库增量日志解析,提供增量数据订阅和消费 早期阿里巴巴因为杭州和美国双机房部署,存在 ...

  4. [论文阅读笔记] Fast Network Embedding Enhancement via High Order Proximity Approximati

    [论文阅读笔记] Fast Network Embedding Enhancement via High Order Proximity Approximation 本文结构 解决问题 主要贡献 主要 ...

  5. python函数默认值只初始化一次

    当在函数中定义默认值时,值初始化只会进行一次,就是执行到def methodname时执行.看下面代码: from datetime import datetime def test(t=dateti ...

  6. OCR-Form-Tools项目试玩记录(一)本地部署

    OCR-Form-Tools是微软的一个开源代码库,包含微软表单识别和OCR服务相关的多种工具.Github仓库地址 目前,表单标注工具(OCR Form Labeling Tool)是首个发布到本该 ...

  7. [bug] Unrecognized token 'code': was expecting (JSON String, Number, Array, Object,'true', 'false' or 'null')

    JSON格式有误,需用JSON.stringify()函数转换一下 参考 https://www.cnblogs.com/sunyanblog/p/13788740.html https://www. ...

  8. 使用CSS样式的三种方法

    一.内联样式 内联样式通过style属性来设置,属性值可以任意的CSS样式. 1 <!DOCTYPE html> 2 <html lang="en"> 3 ...

  9. Understanding User and Kernel Mode

    https://blog.codinghorror.com/understanding-user-and-kernel-mode/ Continue Discussion92 repliesJan ' ...

  10. 测试 USB 存储设备读写性能(Mb/s),平均读写速度等

    1.将U盘(USB3.0)插入被测试机器,假定识别设备为sdc2.创建vfat文件系统分区/dev/sdb1分区容量大于30GBumount /dev/sdc1mkfs -t vfat /dev/sd ...