1
0
mirror of https://github.com/ruanyf/es6tutorial.git synced 2025-05-24 10:22:23 +00:00

修改array

This commit is contained in:
Ruan Yifeng 2015-02-26 01:09:05 +08:00
parent 0c99976064
commit caa4f9e993
4 changed files with 118 additions and 2 deletions

View File

@ -37,6 +37,16 @@ Array.from({ 0: "a", 1: "b", 2: "c", length: 3 });
```
对于还没有部署该方法的浏览器可以用Array.prototyp.slice方法替代。
```javascript
const toArray = (() =>
Array.from ? Array.from : obj => [].slice.call(obj)
)();
```
Array.from()还可以接受第二个参数作用类似于数组的map方法用来对每个元素进行处理。
```JavaScript
@ -170,6 +180,40 @@ for (let [index, elem] of ['a', 'b'].entries()) {
```
## 数组实例的includes()
Array.protypeto.includes方法返回一个布尔值表示某个数组是否包含给定的值。该方法属于ES7。
```javascript
[1, 2, 3].includes(2); // true
[1, 2, 3].includes(4); // false
[1, 2, NaN].includes(NaN); // true
```
该方法的第二个参数表示搜索的起始位置默认为0。
```javascript
[1, 2, 3].includes(3, 3); // false
[1, 2, 3].includes(3, -1); // true
```
下面代码用来检查当前环境是否支持该方法,如果不支持,部署一个简易的替代版本。
```javascript
const contains = (() =>
Array.prototype.includes
? (arr, value) => arr.includes(value)
: (arr, value) => arr.some(el => el === value)
)();
contains(["foo", "bar"], "baz"); // => false
```
## 数组推导
ES6提供简洁写法允许直接通过现有数组生成新数组这被称为数组推导array comprehension

View File

@ -203,7 +203,22 @@ add(2, 5, 3) // 10
上面代码的add函数是一个求和函数利用rest参数可以向该函数传入任意数目的参数。
前面说过rest参数中的变量代表一个数组所以数组特有的方法都可以用于这个变量。下面是一个利用rest参数改写数组push方法的例子。
下面是一个rest参数代替arguments变量的例子。
```javascript
// arguments变量的写法
const sortNumbers = () =>
Array.prototype.slice.call(arguments).sort();
// rest参数的写法
const sortNumbers = (...numbers) => numbers.sort();
```
上面代码的两种写法比较后可以发现rest参数的写法更自然也更简洁。
rest参数中的变量代表一个数组所以数组特有的方法都可以用于这个变量。下面是一个利用rest参数改写数组push方法的例子。
```javascript
@ -510,6 +525,17 @@ var getTempItem = id => ({ id: id, name: "Temp" });
```
箭头函数使得表达更加简洁。
```javascript
const isEven = n => n % 2 == 0;
const square = n => n * n;
```
上面代码只用了两行,就定义了两个简单的工具函数。如果不用箭头函数,可能就要占用多行,而且还不如现在这样写醒目。
箭头函数的一个用处是简化回调函数。
```javascript
@ -585,3 +611,31 @@ var handler = {
由于this在箭头函数中被绑定所以不能用call()、apply()、bind()这些方法去改变this的指向。
长期以来JavaScript语言的this对象一直是一个令人头痛的问题在对象方法中使用this必须非常小心。箭头函数绑定this很大程度上解决了这个困扰。
箭头函数内部还可以再使用箭头函数。下面是一个部署管道机制pipeline的例子。
```javascript
const pipeline = (...funcs) =>
val => funcs.reduce((a, b) => b(a), val);
const plus1 = a => a + 1;
const mult2 = a => a * 2;
const addThenMult = pipeline(plus1, mult2);
addThenMult(5)
// 12
```
上面的代码等同于下面的写法。
```javascript
const plus1 = a => a + 1;
const mult2 = a => a * 2;
mult2(plus1(5))
// 12
```

View File

@ -271,7 +271,23 @@ function clone(origin) {
```
**4为属性指定默认值**
**4合并多个对象**
将多个对象合并到某个对象。
```javascript
const merge =
(target, ...sources) => Object.assign(target, ...sources);
```
如果希望合并后返回一个新对象,可以改写上面函数,对一个空对象合并。
```javascript
const merge =
(...sources) => Object.assign({}, ...sources);
```
**5为属性指定默认值**
```javascript

View File

@ -21,6 +21,8 @@
- Axel Rauschmayer, [ECMAScript 6: whats next for JavaScript?](https://speakerdeck.com/rauschma/ecmascript-6-whats-next-for-javascript-august-2014): 关于ES6新增语法的综合介绍有很多例子
- Toby Ho, [ES6 in io.js](http://davidwalsh.name/es6-io)
- Guillermo Rauch, [ECMAScript 6](http://rauchg.com/2015/ecmascript-6/)
- Charles King, [The power of ECMAScript 6](http://charlesbking.com/power_of_es6/#/)
- Benjamin De Cock, [Frontend Guidelines](https://github.com/bendc/frontend-guidelines): ES6最佳实践
## 语法点