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

rename docs/comprehension to docs/array

This commit is contained in:
ruanyf 2014-05-08 13:06:00 +08:00
parent 52a950dc3e
commit c000abd2c8
6 changed files with 131 additions and 5 deletions

View File

@ -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 ES6提供简洁写法允许直接通过现有数组生成新数组这被称为数组推导array comprehension

View File

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

View File

@ -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 ```javascript
@ -29,6 +61,41 @@ var obj = {
有了这个属性实际上已经不需要通过Object.create()来生成新对象了。 有了这个属性实际上已经不需要通过Object.create()来生成新对象了。
**2Object.setPrototypeOf()**
Object.setPrototypeOf方法的作用与__proto__相同用来设置一个对象的prototype对象。
```javascript
// 格式
Object.setPrototypeOf(object, prototype)
// 用法
var o = Object.setPrototypeOf({}, null);
```
该方法等同于下面的函数。
```javascript
function (obj, proto) {
obj.__proto__ = proto;
return obj;
}
```
**3Object.getPrototypeOf()**
与setPrototypeOf()配套的还有getPrototypeOf()用于读取一个对象的prototype对象。
```javascript
Object.getPrototypeOf(obj)
```
## 增强的对象写法 ## 增强的对象写法
ES6允许直接写入变量和函数作为对象的属性和方法。这样的书写更加简洁。 ES6允许直接写入变量和函数作为对象的属性和方法。这样的书写更加简洁。

View File

@ -43,3 +43,4 @@
- Casper Beyer, [ECMAScript 6 Features and Tools](http://caspervonb.github.io/2014/03/05/ecmascript6-features-and-tools.html) - 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/) - 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模块有详细解释 - 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

View File

@ -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属性和方法** **2属性和方法**
Map数据结构有以下属性和方法。 Map数据结构有以下属性和方法。

View File

@ -9,9 +9,9 @@
1. [ECMAScript 6简介](#docs/intro) 1. [ECMAScript 6简介](#docs/intro)
1. [let和const命令](#docs/let) 1. [let和const命令](#docs/let)
1. [多变量的模式赋值](#docs/destructuring) 1. [多变量的模式赋值](#docs/destructuring)
1. [数组推导](#docs/comprehension)
1. [字符串的扩展](#docs/string) 1. [字符串的扩展](#docs/string)
1. [数值的扩展](#docs/number) 1. [数值的扩展](#docs/number)
1. [数组的扩展](#docs/array)
1. [对象的扩展](#docs/object) 1. [对象的扩展](#docs/object)
1. [函数的扩展](#docs/function) 1. [函数的扩展](#docs/function)
1. [Set和Map数据结构](#docs/set-map) 1. [Set和Map数据结构](#docs/set-map)