ES6常用点

原文    http://es6.ruanyifeng.com/#docs

变量---------------------------------------------

1.let声明、const声明(块级作用域)

字符串-----------------------------------------

2.padStart和padEnd字符串补全

3.模版字符串 ${......} (增强版字符串标记是反引号`)

4.模板编译<%......%>和<%= .....%>

函数---------------------------------------------

5.函数的参数设定默认值

6.箭头函数

var f = () => 5;
// 等同于
var f = function () { return 5 };

var sum = (num1, num2) => num1 + num2;
// 等同于
var sum = function(num1, num2) {
  return num1 + num2;
};

// 无参无返回值
let fn = () => void doesNotReturn();

 注意: 箭头函数导致this总是指向函数定义生效时所在的对象

数组---------------------------------------------

7.扩展运算符...    (将一个数组转为用逗号分隔的参数序列)

 

console.log(...[1, 2, 3])
// 1 2 3

console.log(1, ...[2, 3, 4], 5)
// 1 2 3 4 5

[...document.querySelectorAll('div')]
// [<div>, <div>, <div>]

 

将数组转化为函数的参数

function f(x, y, z) {
  // ...
}

// ES5 的写法
var args = [0, 1, 2];
f.apply(null, args);

// ES6的写法
let args = [0, 1, 2];
f(...args);

数组添加到另一个数组的尾部

// ES6 的写法
let arr1 = [0, 1, 2];
let arr2 = [3, 4, 5];
arr1.push(...arr2);

设置时间

// ES6
new Date(...[2015, 1, 1]);

复制数组

const a1 = [1, 2];

// ES5 只能用变通方法来复制数组。
const a2 = a1.concat();

// ES6复制数组
// 写法一
const a2 = [...a1];
// 写法二
const [...a2] = a1;

合并数组

// ES5
[1, 2].concat(more)
// ES6
[1, 2, ...more]

// ES5的合并数组
arr1.concat(arr2, arr3);
// ES6的合并数组
[...arr1, ...arr2, ...arr3]

字符串转数组

[...'hello']
// [ "h", "e", "l", "l", "o" ]

对象---------------------------------------------

同值相等

Object.is('foo', 'foo')
// true
Object.is({}, {})
// false

不同之处只有两个:一是+0不等于-0,二是NaN等于自身。
+0 === -0 //true
NaN === NaN // false

Object.is(+0, -0) // false
Object.is(NaN, NaN) // true

 

posted @ 2018-02-28 15:31  最好的年纪  阅读(145)  评论(0编辑  收藏  举报