Skip to content

Commit c000abd

Browse files
committed
rename docs/comprehension to docs/array
1 parent 52a950d commit c000abd

File tree

6 files changed

+131
-5
lines changed

6 files changed

+131
-5
lines changed

docs/comprehension.md renamed to docs/array.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,30 @@
1-
# 数组推导
1+
# 数组的扩展
2+
3+
## Array.prototype.find(),Array.prototype.findIndex()
4+
5+
Array.prototype.find()用于找出第一个符合条件的数组元素。它的参数是一个回调函数,所有数组元素依次遍历该回调函数,直到找出第一个返回值为true的元素。
6+
7+
```javascript
8+
9+
[1, 5, 10, 15].find(function(value, index, arr) {
10+
return a > 9;
11+
}) // 10
12+
13+
```
14+
15+
从上面代码可以看到,回调函数接受三个参数,依次为当前的值、当前的位置和原数组。
16+
17+
Array.prototype.findIndex()的用法与find()非常类似,返回第一个符合条件的数组元素的位置。
18+
19+
```javascript
20+
21+
[1, 5, 10, 15].findIndex(function(value, index, arr) {
22+
return a > 9;
23+
}) // 2
24+
25+
```
26+
27+
## 数组推导
228

329
ES6提供简洁写法,允许直接通过现有数组生成新数组,这被称为数组推导(array comprehension)。
430

docs/number.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ ES6在Math对象上提供了更多的数学方法。
7878
- Math.cosh(x) 返回x的双曲余弦(hyperbolic cosine)
7979
- Math.expm1(x) 返回eˆx - 1
8080
- Math.fround(x) 返回x的单精度浮点数形式
81-
- Math.hypot(...values) 返回所有参数的平方的和的平方根
81+
- Math.hypot(...values) 返回所有参数的平方和的平方根
8282
- Math.imul(x, y) 返回两个参数以32位整数形式相乘的结果
8383
- Math.log1p(x) 返回1 + x的自然对数
8484
- Math.log10(x) 返回以10为底的x的对数

docs/object.md

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,41 @@ Object.is(NaN, NaN) // true
1414

1515
```
1616

17-
## __proto__属性
17+
## Object.assign()
1818

19-
ES6正式将__proto__属性写入了标准,用来指定对象的prototype对象。
19+
Object.assign()用来将源对象(source)的所有可枚举属性,复制到目标对象(target)。它至少需要两个对象作为参数,第一个参数是目标对象,后面的参数都是源对象。只要有一个参数不是对象,就会抛出TypeError错误。
20+
21+
```javascript
22+
23+
var target = { a: 1 };
24+
25+
var source1 = { b: 2 };
26+
var source2 = { c: 3 };
27+
28+
Object.assign(target, source1, source2);
29+
target // {a:1, b:2, c:3}
30+
31+
```
32+
33+
注意,如果源对象与目标对象有同名属性,则前者会覆盖后者。
34+
35+
```javascript
36+
37+
var target = { a: 1, b: 1 };
38+
39+
var source1 = { b: 2 };
40+
var source2 = { c: 3 };
41+
42+
Object.assign(target, source1, source2);
43+
target // {a:1, b:2, c:3}
44+
45+
```
46+
47+
## __proto__属性,Object.setPrototypeOf(),Object.getPrototypeOf()
48+
49+
**(1)__proto__属性**
50+
51+
__proto__属性,用来读取或设置当前对象的prototype对象。该属性一度被正式写入ES6草案,但后来又被移除。目前,所有浏览器(包括IE11)都部署了这个属性。
2052

2153
```javascript
2254

@@ -29,6 +61,41 @@ var obj = {
2961

3062
有了这个属性,实际上已经不需要通过Object.create()来生成新对象了。
3163

64+
**(2)Object.setPrototypeOf()**
65+
66+
Object.setPrototypeOf方法的作用与__proto__相同,用来设置一个对象的prototype对象。
67+
68+
```javascript
69+
70+
// 格式
71+
Object.setPrototypeOf(object, prototype)
72+
73+
// 用法
74+
var o = Object.setPrototypeOf({}, null);
75+
76+
```
77+
78+
该方法等同于下面的函数。
79+
80+
```javascript
81+
82+
function (obj, proto) {
83+
obj.__proto__ = proto;
84+
return obj;
85+
}
86+
87+
```
88+
89+
**(3)Object.getPrototypeOf()**
90+
91+
与setPrototypeOf()配套的,还有getPrototypeOf(),用于读取一个对象的prototype对象。
92+
93+
```javascript
94+
95+
Object.getPrototypeOf(obj)
96+
97+
```
98+
3299
## 增强的对象写法
33100

34101
ES6允许直接写入变量和函数,作为对象的属性和方法。这样的书写更加简洁。

docs/reference.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,4 @@
4343
- Casper Beyer, [ECMAScript 6 Features and Tools](http://caspervonb.github.io/2014/03/05/ecmascript6-features-and-tools.html)
4444
- Stoyan Stefanov, [Writing ES6 today with jstransform](http://www.phpied.com/writing-es6-today-with-jstransform/)
4545
- ES6 Module Loader, [ES6 Module Loader Polyfill](https://github.com/ModuleLoader/es6-module-loader): 在浏览器和node.js加载ES6模块的一个库,文档里对ES6模块有详细解释
46+
- Paul Miller, [es6-shim](https://github.com/paulmillr/es6-shim): 一个针对老式浏览器,模拟ES6部分功能的垫片库(shim)

docs/set-map.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,38 @@ map.get("title") // "Author"
153153

154154
```
155155

156+
注意,只有对同一个对象的引用,Map结构才将其视为同一个键。这一点要非常小心。
157+
158+
```javascript
159+
160+
var map = new Map();
161+
162+
map.set(['a'], 555);
163+
map.get(['a']) // undefined
164+
165+
```
166+
167+
上面代码的set和get方法,表面是针对同一个键,但实际上这是两个值,内存地址是不一样的,因此get方法无法读取该键,返回undefined。
168+
169+
同理,同样的值的两个实例,在Map结构中被视为两个键。
170+
171+
```javascript
172+
173+
var map = new Map();
174+
175+
var k1 = ['a'];
176+
var k2 = ['a'];
177+
178+
map.set(k1, 111);
179+
map.set(k2, 222);
180+
181+
map.get(k1) // 111
182+
map.get(k2) // 222
183+
184+
```
185+
186+
上面代码中,变量k1和k2的值是一样的,但是它们在Map结构中被视为两个键。
187+
156188
**(2)属性和方法**
157189

158190
Map数据结构有以下属性和方法。

sidebar.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
1. [ECMAScript 6简介](#docs/intro)
1010
1. [let和const命令](#docs/let)
1111
1. [多变量的模式赋值](#docs/destructuring)
12-
1. [数组推导](#docs/comprehension)
1312
1. [字符串的扩展](#docs/string)
1413
1. [数值的扩展](#docs/number)
14+
1. [数组的扩展](#docs/array)
1515
1. [对象的扩展](#docs/object)
1616
1. [函数的扩展](#docs/function)
1717
1. [Set和Map数据结构](#docs/set-map)

0 commit comments

Comments
 (0)