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

修改numbers

This commit is contained in:
Ruan Yifeng 2015-01-13 09:51:16 +08:00
parent 540642f75d
commit 29149c7450
5 changed files with 229 additions and 16 deletions

View File

@ -16,6 +16,27 @@ Array.from(ps).forEach(function (p) {
上面代码中querySelectorAll方法返回的是一个类似数组的对象只有将这个对象转为真正的数组才能使用forEach方法。
Array.from方法可以将函数的arguments对象转为数组。
```javascript
function foo() {
var args = Array.from( arguments );
}
foo( "a", "b", "c" );
```
任何有length属性的对象都可以通过Array.from方法转为数组。
```javascript
Array.from({ 0: "a", 1: "b", 2: "c", length: 3 });
// [ "a", "b" , "c" ]
```
Array.from()还可以接受第二个参数作用类似于数组的map方法用来对每个元素进行处理。
```JavaScript
@ -43,11 +64,12 @@ Array.of()方法用于将一组值,转换为数组。
```javaScript
Array.of(3, 11, 8) // [3,11,8]
Array.of(3) // [3]
Array.of(3).length // 1
```
这个函数的主要目的是弥补数组构造函数Array()的不足。因为参数个数的不同会导致Array()的行为有差异。
这个方法的主要目的是弥补数组构造函数Array()的不足。因为参数个数的不同会导致Array()的行为有差异。
```javascript

View File

@ -199,6 +199,16 @@ var x;
```
对象的解构赋值,可以很方便地将现有对象的方法,赋值到某个变量。
```javascript
let { log, sin, cos } = Math;
```
上面代码将Math对象的对数、正弦、余弦三个方法赋值到对应的变量上使用起来就会方便很多。
## 用途
变量的解构赋值用途很多。

View File

@ -208,6 +208,16 @@ function f(a, ...b, c) {
```
函数的length属性不包括rest参数。
```javascript
(function(a) {}).length // 1
(function(...a) {}).length // 0
(function(a, ...b) {}).length // 1
```
## 扩展运算符
扩展运算符spread是三个点...。它好比rest参数的逆运算将一个数组转为用逗号分隔的参数序列。该运算符主要用于函数调用。

View File

@ -17,7 +17,71 @@ ES6提供了二进制和八进制数值的新的写法分别用前缀0b和0o
ES6在Number对象上新提供了Number.isFinite()和Number.isNaN()两个方法用来检查Infinite和NaN这两个特殊值。
它们与传统的isFinite()和isNaN()的区别在于传统方法先调用Number()将非数值的值转为数值再进行判断而这两个新方法只对数值有效非数值一律返回false。
Number.isFinite()用来检查一个数值是否非无穷infinity
```javascript
Number.isFinite(15); // true
Number.isFinite(0.8); // true
Number.isFinite(NaN); // false
Number.isFinite(Infinity); // false
Number.isFinite(-Infinity); // false
Number.isFinite("foo"); // false
Number.isFinite("15"); // false
Number.isFinite(true); // false
```
ES5通过下面的代码部署Number.isFinite方法。
```javascript
(function (global) {
var global_isFinite = global.isFinite;
Object.defineProperty(Number, 'isFinite', {
value: function isFinite(value) {
return typeof value === 'number' && global_isFinite(value);
},
configurable: true,
enumerable: false,
writable: true
});
})(this);
```
Number.isNaN()用来检查一个值是否为NaN。
```javascript
Number.isNaN(NaN); // true
Number.isNaN(15); // false
Number.isNaN("15"); // false
Number.isNaN(true); // false
```
ES5通过下面的代码部署Number.isNaN()。
```javascript
(function (global) {
var global_isNaN = global.isNaN;
Object.defineProperty(Number, 'isNaN', {
value: function isNaN(value) {
return typeof value === 'number' && global_isNaN(value);
},
configurable: true,
enumerable: false,
writable: true
});
})(this);
```
它们与传统的全局方法isFinite()和isNaN()的区别在于传统方法先调用Number()将非数值的值转为数值再进行判断而这两个新方法只对数值有效非数值一律返回false。
```javascript
@ -60,6 +124,30 @@ Number.isInteger()用来判断一个值是否为整数。需要注意的是,
Number.isInteger(25) // true
Number.isInteger(25.0) // true
Number.isInteger(25.1) // false
Number.isInteger("15") // false
Number.isInteger(true) // false
```
ES5通过下面的代码部署Number.isInteger()。
```javascript
(function (global) {
var floor = Math.floor,
isFinite = global.isFinite;
Object.defineProperty(Number, 'isInteger', {
value: function isInteger(value) {
return typeof value === 'number' && isFinite(value) &&
value > -9007199254740992 && value < 9007199254740992 &&
floor(value) === value;
},
configurable: true,
enumerable: false,
writable: true
});
})(this);
```
@ -93,7 +181,47 @@ Math.trunc(-4.9) // -4
```
**2数学方法**
**2Math.sign()**
Math.sign方法用来判断一个数到底是正数、负数、还是零。如果参数为正数返回+1参数为负数返回-1参数为0返回0参数为NaN返回NaN。
```javascript
Math.sign(-5) // -1
Math.sign(5) // +1
Math.sign(0) // +0
Math.sign(-) // -0
Math.sign(NaN) // NaN
```
ES5通过下面的代码可以部署Math.sign()。
```javascript
(function (global) {
var isNaN = Number.isNaN;
Object.defineProperty(Math, 'sign', {
value: function sign(value) {
var n = +value;
if (isNaN(n))
return n /* NaN */;
if (n === 0)
return n; // Keep the sign of the zero.
return (n < 0) ? -1 : 1;
},
configurable: true,
enumerable: false,
writable: true
});
})(this);
```
**3数学方法**
ES6在Math对象上还提供了许多新的数学方法。
@ -110,5 +238,4 @@ ES6在Math对象上还提供了许多新的数学方法。
- Math.log1p(x) 返回1 + x的自然对数
- Math.log10(x) 返回以10为底的x的对数
- Math.log2(x) 返回以2为底的x的对数
- Math.sign(x) 如果x为负返回-1x为0返回0x为正返回1
- Math.tanh(x) 返回x的双曲正切hyperbolic tangent

View File

@ -6,6 +6,42 @@ ES6允许直接写入变量和函数作为对象的属性和方法。这样
```javascript
function f( x, y ) {
return { x, y };
}
// 等同于
function f( x, y ) {
return { x: x, y: y };
}
```
上面是属性简写的例子,方法也可以简写。
```javascript
var o = {
method() {
return "Hello!";
}
};
// 等同于
var o = {
method: function() {
return "Hello!";
}
};
```
下面是一个更实际的例子。
```javascript
var Person = {
name: '张三',
@ -36,18 +72,6 @@ getPoint()
```
下面是一个类似的例子。
```javascript
let x = 4;
let y = 1;
// 下行等同于 let obj = { x: x, y: y };
let obj = { x, y };
```
## 属性名表达式
JavaScript语言定义对象的属性有两种方法。
@ -133,6 +157,26 @@ Object.is(NaN, NaN) // true
```
ES5可以通过下面的代码部署Object.is()。
```javascript
Object.defineProperty(Object, 'is', {
value: function(x, y) {
if (x === y) {
// 针对+0 不等于 -0的情况
return x !== 0 || 1 / x === 1 / y;
}
// 针对NaN的情况
return x !== x && y !== y;
},
configurable: true,
enumerable: false,
writable: true
});
```
## Object.assign()
Object.assign方法用来将源对象source的所有可枚举属性复制到目标对象target。它至少需要两个对象作为参数第一个参数是目标对象后面的参数都是源对象。只要有一个参数不是对象就会抛出TypeError错误。