以下内容只用于个人复习巩固所用,方便快速回忆,具体用法和注意参数大家可以去阮一峰老师的博客去自行查看 ECMAScript 6 入门
1. let 和 const 命令
es6 新增了块级作用域。
let 所声明的变量只在let代码块有效 ,适用于for循环
const 声明的是常量 定义即赋值
2. 变量的解构赋值
1. 数组: let [a, b, c] = [1, 2, 3];
2. 对象:let { foo, bar } = { foo: "aaa", bar: "bbb" };
3. 字符串:const [a, b, c, d, e] = 'hello';
4. 数值和布尔值:let {toString: s} = 123;
s === Number.prototype.toString // true
let {toString: s} = true;
s === Boolean.prototype.toString // true
5.函数的参数:function add([x, y]){
return x + y;
}
add([1, 2]); // 3
3.字符串的扩展
1. 模板字符串
$('#result').append(`
There are <b>${basket.count}</b> items
in your basket, <em>${basket.onSale}</em>
are on sale!
`);
2. 模板编译
let template = `
<ul>
<% for(let i=0; i < data.supplies.length; i++) { %>
<li><%= data.supplies[i] %></li>
<% } %>
</ul>
`;
3.标签模板 ${ }
tag`Hello ${ a + b } world ${ a * b }`;
4. 正则的扩展
1.RegExp构造函数
var regex = new RegExp('xyz', 'i');
// 等价于
var regex = /xyz/i;
2. 字符串的正则方法
可以使用正则表达式:match()、replace()、search()和split()。
5.数组的扩展
1.扩展运算符
console.log(...[1, 2, 3])
// 1 2 3
6.Set 和 Map 数据结构
1.set
// 例一
const set = new Set([1, 2, 3, 4, 4]);
[...set]
// [1, 2, 3, 4]
// 例二
const items = new Set([1, 2, 3, 4, 5, 5, 5, 5]);
items.size // 5
// 例三
const set = new Set(document.querySelectorAll('div'));
set.size // 56
实用方法
数组去重: [...new Set(array)]
字符串去重:[...new Set('ababbc')].join('') // "abc"
-
Map