diff --git a/docs/array.md b/docs/array.md index 23bac7a..fcb92a2 100644 --- a/docs/array.md +++ b/docs/array.md @@ -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 diff --git a/docs/destructuring.md b/docs/destructuring.md index 36ad9e1..a6db313 100644 --- a/docs/destructuring.md +++ b/docs/destructuring.md @@ -199,6 +199,16 @@ var x; ``` +对象的解构赋值,可以很方便地将现有对象的方法,赋值到某个变量。 + +```javascript + +let { log, sin, cos } = Math; + +``` + +上面代码将Math对象的对数、正弦、余弦三个方法,赋值到对应的变量上,使用起来就会方便很多。 + ## 用途 变量的解构赋值用途很多。 diff --git a/docs/function.md b/docs/function.md index 8ecfd9f..a8a1bb8 100644 --- a/docs/function.md +++ b/docs/function.md @@ -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参数的逆运算,将一个数组转为用逗号分隔的参数序列。该运算符主要用于函数调用。 diff --git a/docs/number.md b/docs/number.md index 28a321e..828f23b 100644 --- a/docs/number.md +++ b/docs/number.md @@ -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)数学方法** +**(2)Math.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为负返回-1,x为0返回0,x为正返回1 - Math.tanh(x) 返回x的双曲正切(hyperbolic tangent) diff --git a/docs/object.md b/docs/object.md index 11408ce..f6451fc 100644 --- a/docs/object.md +++ b/docs/object.md @@ -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错误。