首页 技术 正文
技术 2022年11月16日
0 收藏 627 点赞 2,201 浏览 7768 个字

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/

<!–
/* GitHub stylesheet for MarkdownPad (http://markdownpad.com) */
/* Author: Nicolas Hery – http://nicolashery.com */
/* Version: b13fe65ca28d2e568c6ed5d7f06581183df8f2ff */
/* Source: https://github.com/nicolahery/markdownpad-github */

/* RESET
=============================================================================*/

html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
}

/* BODY
=============================================================================*/

body {
font-family: Helvetica, arial, freesans, clean, sans-serif;
font-size: 14px;
line-height: 1.6;
color: #333;
background-color: #fff;
padding: 20px;
margin: 0 auto;
}

body>–>*: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%
}
–>

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,557
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898