0、导言

最近从coffee切换到js,代码量一下子变大了不少,也多了些许陌生感。为了在JS代码中,更合理的使用ES6的新特性,特在此对ES6的特性做一个简单的总览。

1、模块(Module) --Chrome测试不可用

在ES6中,有class的概念,不过这只是语法糖,并没有解决模块化问题。Module功能则是为了解决模块化问题而提出的。

我们可以使用如下方式定义模块:

11_lib.js文件内容

// 导出属性和方法
export var PI = 3.1415926;
export function calcCircularArea(r){
return PI * r * r;
}

app.js文件内容

//导出所有,使用别名调用
import * as lib from '11_lib';
console.log(lib.calcCircularArea(2));
console.log(lib.PI); //导出属性和方法
import {calcCircularArea, PI} from '11_lib';
console.log(calcCircularArea(2));
console.log(PI);

2、模块加载器(Module Loaders) --Chrome测试不可用

既然用了定义module的规范,那么也就需要一个模块加载器,需要支持如下内容:

  1. 动态加载
  2. 状态隔离
  3. 全局命名空间隔离
  4. 编译钩子
  5. 嵌套虚拟化

System.import('11_lib').then(function(m) {
console.log(m.calcCircularArea(2));
console.log(m.PI);
});

3、图 + 集合 + 弱引用图 + 若引用集合(Map + Set + WeakMap + WeakSet)

在ES6中,新增了几种数据结构。

Map是一种类似于Object的结构,Object本质上是键值对的集合,但是只能是字符串当做键,所以有一定的使用限制。Map的话,则可以拿任意类型来作为key。具体使用如下:

var map = new Map();
var key = {key: 'hah'};
map.set(key, '1'); //设置key-value
map.set(key, 2); //对已有key进行设置,表示覆盖
console.log(map.get(key)); //获取key的值
console.log(map.size);//获取map的元素个数
map.has(key); //判断map中有指定的key
map.delete(key); //删除map中指定的key
map.clear(); //清空map

WeakMap和Map是比较类似的,唯一的区别是只接受对象作为键名(null除外),而且键名所指向的对象,不计入垃圾回收机制。

Set是一种类似于数组,但成员的值都是唯一(引用唯一)的一种数据结构。具体使用如下:

var set = new Set();//定义set
set.add(1).add(2).add(1).add('2'); //添加数据
console.log(set.size);//查看set中元素的数量,结果应该是3,因为重复添加不计算,2和'2'不等。
set.delete(1); //删除set的值(通过value删除)。
set.has(1); //set是否包含某个value
set.keys(); //返回set的所有key
set.values(); //返回set的所有value
set.clear();//清空set

WeakSet和Set也是比较类型的,和Set有两个区别,一个是成员只能是对象;二个是WeakSet是不可遍历的。

4、代理(Proxies) --Chrome测试不可用

代理允许用宿主的行为来创建对象,能够实现拦截,对象的虚拟化,日志和分析等功能。

5、数据类型Symbols

在ES5中,JS只有6中原始类型,在ES6中,新增了Symbols类型,成为了JS中的第7种原始类型。 该类型表示独一无二的值。使用如下:

var key = Symbol(); //定义Symbol对象 console.log(typeof key); //symbol ,表示为类型,而且不是string类型的。 key = Symbol('这是一个说明'); //可以在定义Symbol的时候,添加一个说明

Symbol不能与其他类型值进行运算,但是可以显式转换为字符串,和转换为布尔值

console.log(key.toString());
console.log(String(key)); if(key){
console.log('key is true');
}

在对象的内部,要使用Symbol值定义属性时,必须放在方括号中。

var obj = {
[key]: 'abc'
};

6、可以子类化的内置对象

在ES6中,我们可以自定义类型来继承内置对象,这个时候,如果要自定义构造函数,必须要在构造函数中调用super(),来呼叫父类的构造。

'use strict';
class MyArray extends Array {
// 如果要定义constuctor,那么就必须要使用super来执行父类的构造
constructor(){
super();
}
} var arr = new MyArray();
arr[1] = 12;
console.log(arr.length === 2);

7、新增的API(Math + Number + String + Array + Object APIs)

如下代码,一目了然:

//数字类api
Number.EPSILON; //增加常量e
Number.isInteger(Infinity) // false
Number.isNaN("NaN") // false //数学类api
Math.acosh(3) // 1.762747174039086
Math.hypot(3, 4) // 5
Math.imul(Math.pow(2, 32) - 1, Math.pow(2, 32) - 2) // 2 //字符串类api
"abcde".includes("cd") // true
"abc".repeat(3) // "abcabcabc" //数组api
Array.from(document.querySelectorAll('*')) // Returns a real Array
Array.of(1, 2, 3) // Similar to new Array(...), but without special one-arg behavior
[0, 0, 0].fill(7, 1) // [0,7,7]
[1, 2, 3].find(x => x == 3) // 3
[1, 2, 3].findIndex(x => x == 2) // 1
[1, 2, 3, 4, 5].copyWithin(3, 0) // [1, 2, 3, 1, 2]
["a", "b", "c"].entries() // iterator [0, "a"], [1,"b"], [2,"c"]
["a", "b", "c"].keys() // iterator 0, 1, 2
["a", "b", "c"].values() // iterator "a", "b", "c" //对象api
Object.assign(Point, { origin: new Point(0,0) })

8、二进制和八进制字面量(Binary and Octal Literals)

直接上示例:

var n1 = 0b111110101; //0b前缀,表示二进制字面量
console.log(n1); //输出的时候,直接用10进制展示 var n2 = 0o12345; //0o前缀,表示八进制字面量
console.log(n2);

9、承诺(Promises)

Promise是ES6中新增的异步编程库。

//使用承诺定义一个异步任务
var p = new Promise((resolve, reject)=>{
return setTimeout(function(){
reject('ok');
}, 2000);
}); p.then((data)=>{
console.log(data);
}, (data)=>{
console.log('error' + data);
}).then(()=>{
console.log('throw err');
throw 'Error';
}).catch(err => {
console.log(err);
});

10、参考资料

1、ECMAScript 6 features https://github.com/lukehoban/es6features

2、ECMAScript 6 入门 http://es6.ruanyifeng.com/

*:first-child {
margin-top: 0 !important;
}

body>*:last-child {
margin-bottom: 0 !important;
}

/* BLOCKS
=============================================================================*/

p, blockquote, ul, ol, dl, table, pre {
margin: 15px 0;
}

/* HEADERS
=============================================================================*/

h1, h2, h3, h4, h5, h6 {
margin: 20px 0 10px;
padding: 0;
font-weight: bold;
-webkit-font-smoothing: antialiased;
}

h1 tt, h1 code, h2 tt, h2 code, h3 tt, h3 code, h4 tt, h4 code, h5 tt, h5 code, h6 tt, h6 code {
font-size: inherit;
}

h1 {
font-size: 28px;
color: #000;
}

h2 {
font-size: 24px;
border-bottom: 1px solid #ccc;
color: #000;
}

h3 {
font-size: 18px;
}

h4 {
font-size: 16px;
}

h5 {
font-size: 14px;
}

h6 {
color: #777;
font-size: 14px;
}

body>h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h4:first-child, body>h5:first-child, body>h6:first-child {
margin-top: 0;
padding-top: 0;
}

a:first-child h1, a:first-child h2, a:first-child h3, a:first-child h4, a:first-child h5, a:first-child h6 {
margin-top: 0;
padding-top: 0;
}

h1+p, h2+p, h3+p, h4+p, h5+p, h6+p {
margin-top: 10px;
}

/* LINKS
=============================================================================*/

a {
color: #4183C4;
text-decoration: none;
}

a:hover {
text-decoration: underline;
}

/* LISTS
=============================================================================*/

ul, ol {
padding-left: 30px;
}

ul li > :first-child,
ol li > :first-child,
ul li ul:first-of-type,
ol li ol:first-of-type,
ul li ol:first-of-type,
ol li ul:first-of-type {
margin-top: 0px;
}

ul ul, ul ol, ol ol, ol ul {
margin-bottom: 0;
}

dl {
padding: 0;
}

dl dt {
font-size: 14px;
font-weight: bold;
font-style: italic;
padding: 0;
margin: 15px 0 5px;
}

dl dt:first-child {
padding: 0;
}

dl dt>:first-child {
margin-top: 0px;
}

dl dt>:last-child {
margin-bottom: 0px;
}

dl dd {
margin: 0 0 15px;
padding: 0 15px;
}

dl dd>:first-child {
margin-top: 0px;
}

dl dd>:last-child {
margin-bottom: 0px;
}

/* CODE
=============================================================================*/

pre, code, tt {
font-size: 12px;
font-family: Consolas, "Liberation Mono", Courier, monospace;
}

code, tt {
margin: 0 0px;
padding: 0px 0px;
white-space: nowrap;
border: 1px solid #eaeaea;
background-color: #f8f8f8;
border-radius: 3px;
}

pre>code {
margin: 0;
padding: 0;
white-space: pre;
border: none;
background: transparent;
}

pre {
background-color: #f8f8f8;
border: 1px solid #ccc;
font-size: 13px;
line-height: 19px;
overflow: auto;
padding: 6px 10px;
border-radius: 3px;
}

pre code, pre tt {
background-color: transparent;
border: none;
}

kbd {
-moz-border-bottom-colors: none;
-moz-border-left-colors: none;
-moz-border-right-colors: none;
-moz-border-top-colors: none;
background-color: #DDDDDD;
background-image: linear-gradient(#F1F1F1, #DDDDDD);
background-repeat: repeat-x;
border-color: #DDDDDD #CCCCCC #CCCCCC #DDDDDD;
border-image: none;
border-radius: 2px 2px 2px 2px;
border-style: solid;
border-width: 1px;
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
line-height: 10px;
padding: 1px 4px;
}

/* QUOTES
=============================================================================*/

blockquote {
border-left: 4px solid #DDD;
padding: 0 15px;
color: #777;
}

blockquote>:first-child {
margin-top: 0px;
}

blockquote>:last-child {
margin-bottom: 0px;
}

/* HORIZONTAL RULES
=============================================================================*/

hr {
clear: both;
margin: 15px 0;
height: 0px;
overflow: hidden;
border: none;
background: transparent;
border-bottom: 4px solid #ddd;
padding: 0;
}

/* IMAGES
=============================================================================*/

img {
max-width: 100%
}
-->

ES6入门系列三(特性总览下)的更多相关文章

  1. C# 互操作性入门系列(三):平台调用中的数据封送处理

    好文章搬用工模式启动ing ..... { 文章中已经包含了原文链接 就不再次粘贴了 言明 改文章是一个系列,但只收录了2篇,原因是 够用了 } --------------------------- ...

  2. [转]C# 互操作性入门系列(三):平台调用中的数据封送处理

    参考网址:https://www.cnblogs.com/FongLuo/p/4512738.html C#互操作系列文章: C# 互操作性入门系列(一):C#中互操作性介绍 C# 互操作性入门系列( ...

  3. mybatis入门系列三之类型转换器

    mybatis入门系列三之类型转换器 类型转换器介绍 mybatis作为一个ORM框架,要求java中的对象与数据库中的表记录应该对应 因此java类名-数据库表名,java类属性名-数据库表字段名, ...

  4. ActiveMQ入门系列三:发布/订阅模式

    在上一篇<ActiveMQ入门系列二:入门代码实例(点对点模式)>中提到了ActiveMQ中的两种模式:点对点模式(PTP)和发布/订阅模式(Pub & Sub),详细介绍了点对点 ...

  5. ES6 入门系列 (三) 尾递归

    递归我们不陌生, 那什么是尾递归呢? 为什么要用尾递归呢? 尾递归怎么用呢? 带着这三个问题我们来了解它, 我们知道递归非常耗费内存,一不小心就会发生‘栈溢出’, 相信你一定遇到过这个错误: stac ...

  6. ES6 入门系列 - 函数的扩展

    1函数参数的默认值 基本用法 在ES6之前,不能直接为函数的参数指定默认值,只能采用变通的方法. function log(x, y) { y = y || 'World'; console.log( ...

  7. ES6入门系列四(测试题分析)

    0.导言 ES6中新增了不少的新特性,来点测试题热热身.具体题目来源请看:http://perfectionkills.com/javascript-quiz-es6/. 以下将一题一题来解析what ...

  8. ES6 入门系列 - let 和 const 命令

    let命令 基本用法 ES6新增了let命令,用来声明变量.它的用法类似于var,但是所声明的变量,只在let命令所在的代码块内有效. { let a = ; ; } a // ReferenceEr ...

  9. ES6 入门系列 (一)ES6的前世今生

    要学好javascript , ECMAScript标准比什么都强, ESMAScript标准已经用最严谨的语言和最完美的角度展现了语言的实质和特性. 理解语言的本质后,你已经从沙堆里挑出了珍珠,能经 ...

随机推荐

  1. ORA-12705问题解决过程

    最近开发C#加ORACLE的程序,就有一台电脑连接的时候报错误 ORA-12705: Cannot access NLS data files or invalid environment speci ...

  2. [1008]harder_prime

    素数定义:一个大于1的整数,如果它的约数如果只有1和它本身,那么它就是一个素数. 回文数定义:一个整数把它的各位数字倒过来还是它本身,那么它就是回文数,比如说2,99,393. 回文素数定义:一个数如 ...

  3. 理解一下单片机的I2C和SPI通信

    应某位网友要求,今天说一下单片机的I2C SPI通信,可能说不清楚,因为这毕竟要做实验才可完全理解. I2C和SPI是两种不同的通信协议. 听到协议,似乎高不可攀,其实协议就是人们定义的一个标准而已, ...

  4. MediaWiki安装与配置(Ubuntu 10.4)

    实验室准备发布一个网站,本来是准备外包给别人做的,后来自己调研了一下,发现也没有想象的复杂和困难,于是最近一周自己吭哧吭哧地把网站搭好了. 之所以使用Mediawiki,一是考虑到是以实验室发布,不想 ...

  5. HK一行所见闻

    香港一行 20多年来,未未去过HK,前段时间由于工作关系去了趟HK.感触良多. 一清早,福田过关,做火车,做地铁,一通到了目的地. 总结对那边的印象: 1,所有人都是粤语,包括工作交流.而且他们不怎么 ...

  6. python学习笔记:Day02

    一.列表(list) 1.定义一个列表 name=["tom","jerry","12","13","lose ...

  7. node.js-session问题

    在使用express使用session时发现怎么使用session都是undefined最后发现 app.use(express.cookieParser()); app.use(express.se ...

  8. [.NET领域驱动设计实战系列]专题一:前期准备之EF CodeFirst

    一.前言 从去年已经接触领域驱动设计(Domain-Driven Design)了,当时就想自己搭建一个DDD框架,所以当时看了很多DDD方面的书,例如领域驱动模式与实战,领域驱动设计:软件核心复杂性 ...

  9. 【Bugly干货分享】老司机教你 “飙” EventBus 3

    Bugly 技术干货系列内容主要涉及移动开发方向,是由 Bugly 邀请腾讯内部各位技术大咖,通过日常工作经验的总结以及感悟撰写而成,内容均属原创,转载请标明出处. EventBus对于Android ...

  10. Html5 舞动的雨伞

    HMTL5的学习断断续续,方法不用又生疏了,昨天做的一个雨伞的Demo,先看看效果 主要是运用了中心点变换和旋转两个方法.不同的动画用定时器控制, 下面是全部代码: <canvas id=&qu ...