mirror of
https://github.com/ruanyf/es6tutorial.git
synced 2025-05-24 10:22:23 +00:00
rename docs/comprehension to docs/array
This commit is contained in:
parent
52a950dc3e
commit
c000abd2c8
@ -1,4 +1,30 @@
|
||||
# 数组推导
|
||||
# 数组的扩展
|
||||
|
||||
## Array.prototype.find(),Array.prototype.findIndex()
|
||||
|
||||
Array.prototype.find()用于找出第一个符合条件的数组元素。它的参数是一个回调函数,所有数组元素依次遍历该回调函数,直到找出第一个返回值为true的元素。
|
||||
|
||||
```javascript
|
||||
|
||||
[1, 5, 10, 15].find(function(value, index, arr) {
|
||||
return a > 9;
|
||||
}) // 10
|
||||
|
||||
```
|
||||
|
||||
从上面代码可以看到,回调函数接受三个参数,依次为当前的值、当前的位置和原数组。
|
||||
|
||||
Array.prototype.findIndex()的用法与find()非常类似,返回第一个符合条件的数组元素的位置。
|
||||
|
||||
```javascript
|
||||
|
||||
[1, 5, 10, 15].findIndex(function(value, index, arr) {
|
||||
return a > 9;
|
||||
}) // 2
|
||||
|
||||
```
|
||||
|
||||
## 数组推导
|
||||
|
||||
ES6提供简洁写法,允许直接通过现有数组生成新数组,这被称为数组推导(array comprehension)。
|
||||
|
@ -78,7 +78,7 @@ ES6在Math对象上提供了更多的数学方法。
|
||||
- Math.cosh(x) 返回x的双曲余弦(hyperbolic cosine)
|
||||
- Math.expm1(x) 返回eˆx - 1
|
||||
- Math.fround(x) 返回x的单精度浮点数形式
|
||||
- Math.hypot(...values) 返回所有参数的平方的和的平方根
|
||||
- Math.hypot(...values) 返回所有参数的平方和的平方根
|
||||
- Math.imul(x, y) 返回两个参数以32位整数形式相乘的结果
|
||||
- Math.log1p(x) 返回1 + x的自然对数
|
||||
- Math.log10(x) 返回以10为底的x的对数
|
||||
|
@ -14,9 +14,41 @@ Object.is(NaN, NaN) // true
|
||||
|
||||
```
|
||||
|
||||
## __proto__属性
|
||||
## Object.assign()
|
||||
|
||||
ES6正式将__proto__属性写入了标准,用来指定对象的prototype对象。
|
||||
Object.assign()用来将源对象(source)的所有可枚举属性,复制到目标对象(target)。它至少需要两个对象作为参数,第一个参数是目标对象,后面的参数都是源对象。只要有一个参数不是对象,就会抛出TypeError错误。
|
||||
|
||||
```javascript
|
||||
|
||||
var target = { a: 1 };
|
||||
|
||||
var source1 = { b: 2 };
|
||||
var source2 = { c: 3 };
|
||||
|
||||
Object.assign(target, source1, source2);
|
||||
target // {a:1, b:2, c:3}
|
||||
|
||||
```
|
||||
|
||||
注意,如果源对象与目标对象有同名属性,则前者会覆盖后者。
|
||||
|
||||
```javascript
|
||||
|
||||
var target = { a: 1, b: 1 };
|
||||
|
||||
var source1 = { b: 2 };
|
||||
var source2 = { c: 3 };
|
||||
|
||||
Object.assign(target, source1, source2);
|
||||
target // {a:1, b:2, c:3}
|
||||
|
||||
```
|
||||
|
||||
## __proto__属性,Object.setPrototypeOf(),Object.getPrototypeOf()
|
||||
|
||||
**(1)__proto__属性**
|
||||
|
||||
__proto__属性,用来读取或设置当前对象的prototype对象。该属性一度被正式写入ES6草案,但后来又被移除。目前,所有浏览器(包括IE11)都部署了这个属性。
|
||||
|
||||
```javascript
|
||||
|
||||
@ -29,6 +61,41 @@ var obj = {
|
||||
|
||||
有了这个属性,实际上已经不需要通过Object.create()来生成新对象了。
|
||||
|
||||
**(2)Object.setPrototypeOf()**
|
||||
|
||||
Object.setPrototypeOf方法的作用与__proto__相同,用来设置一个对象的prototype对象。
|
||||
|
||||
```javascript
|
||||
|
||||
// 格式
|
||||
Object.setPrototypeOf(object, prototype)
|
||||
|
||||
// 用法
|
||||
var o = Object.setPrototypeOf({}, null);
|
||||
|
||||
```
|
||||
|
||||
该方法等同于下面的函数。
|
||||
|
||||
```javascript
|
||||
|
||||
function (obj, proto) {
|
||||
obj.__proto__ = proto;
|
||||
return obj;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
**(3)Object.getPrototypeOf()**
|
||||
|
||||
与setPrototypeOf()配套的,还有getPrototypeOf(),用于读取一个对象的prototype对象。
|
||||
|
||||
```javascript
|
||||
|
||||
Object.getPrototypeOf(obj)
|
||||
|
||||
```
|
||||
|
||||
## 增强的对象写法
|
||||
|
||||
ES6允许直接写入变量和函数,作为对象的属性和方法。这样的书写更加简洁。
|
||||
|
@ -43,3 +43,4 @@
|
||||
- Casper Beyer, [ECMAScript 6 Features and Tools](http://caspervonb.github.io/2014/03/05/ecmascript6-features-and-tools.html)
|
||||
- Stoyan Stefanov, [Writing ES6 today with jstransform](http://www.phpied.com/writing-es6-today-with-jstransform/)
|
||||
- ES6 Module Loader, [ES6 Module Loader Polyfill](https://github.com/ModuleLoader/es6-module-loader): 在浏览器和node.js加载ES6模块的一个库,文档里对ES6模块有详细解释
|
||||
- Paul Miller, [es6-shim](https://github.com/paulmillr/es6-shim): 一个针对老式浏览器,模拟ES6部分功能的垫片库(shim)
|
||||
|
@ -153,6 +153,38 @@ map.get("title") // "Author"
|
||||
|
||||
```
|
||||
|
||||
注意,只有对同一个对象的引用,Map结构才将其视为同一个键。这一点要非常小心。
|
||||
|
||||
```javascript
|
||||
|
||||
var map = new Map();
|
||||
|
||||
map.set(['a'], 555);
|
||||
map.get(['a']) // undefined
|
||||
|
||||
```
|
||||
|
||||
上面代码的set和get方法,表面是针对同一个键,但实际上这是两个值,内存地址是不一样的,因此get方法无法读取该键,返回undefined。
|
||||
|
||||
同理,同样的值的两个实例,在Map结构中被视为两个键。
|
||||
|
||||
```javascript
|
||||
|
||||
var map = new Map();
|
||||
|
||||
var k1 = ['a'];
|
||||
var k2 = ['a'];
|
||||
|
||||
map.set(k1, 111);
|
||||
map.set(k2, 222);
|
||||
|
||||
map.get(k1) // 111
|
||||
map.get(k2) // 222
|
||||
|
||||
```
|
||||
|
||||
上面代码中,变量k1和k2的值是一样的,但是它们在Map结构中被视为两个键。
|
||||
|
||||
**(2)属性和方法**
|
||||
|
||||
Map数据结构有以下属性和方法。
|
||||
|
@ -9,9 +9,9 @@
|
||||
1. [ECMAScript 6简介](#docs/intro)
|
||||
1. [let和const命令](#docs/let)
|
||||
1. [多变量的模式赋值](#docs/destructuring)
|
||||
1. [数组推导](#docs/comprehension)
|
||||
1. [字符串的扩展](#docs/string)
|
||||
1. [数值的扩展](#docs/number)
|
||||
1. [数组的扩展](#docs/array)
|
||||
1. [对象的扩展](#docs/object)
|
||||
1. [函数的扩展](#docs/function)
|
||||
1. [Set和Map数据结构](#docs/set-map)
|
||||
|
Loading…
x
Reference in New Issue
Block a user