当前参考学习《JavaScript语言精粹》
JavaScript包含了少量可用在标准类型上的标准方法。
- Array
- Function
- Number
- Object
- RegExp
- String
1、Array
1 // array.concat(item...) 2 // concat方法返回一个新数组,它包含array的浅复制(shallow copy), 3 // 并将1个或多个参数与item附加在其后。 4 // 如果item是一个数组,那么它的每个元素都会被分别添加 5 var a = ['a', 'b', 'c']; 6 var b = ['x', 'y', 'z']; 7 var c = ['1', '2', '3']; 8 var d1 = a.concat(b, true); 9 console.log("d1: " + d1); 10 var d2 = a.concat(b, false); 11 console.log("d2: " + d2); 12 var d3 = a.concat(b, c, true); 13 console.log("d3: " + d3); 14 console.log("----------------1 array.concat(item...) end----------------"); 15 16 // array.join(separator) 17 // join方法是把一个array构造成一个字符串。 18 // 它将array中的每个元素构造成一个字符串 19 // 并用一个separator为分隔符把它们连接在一起 20 // 默认的separator是','。 21 var a = ['a', 'b', 'c']; 22 a.push('d'); 23 var c = a.join(''); 24 console.log("a: " + a); 25 console.log("c: " + c); 26 console.log("----------------2 array.join(separator) end----------------"); 27 28 // array.pop() 29 // pop和push方法使数组array像堆栈(stack)一样工作 30 // pop方法移除array中最后一个元素并返回该元素。如果该array为空,它会返回undefined 31 var a = ['a', 'b', 'c']; 32 var c = a.pop(); 33 console.log("a: " + a); 34 console.log("c: " + c); 35 console.log("----------------3 array.pop() end----------------"); 36 // array.push(item...) 37 // push方法将一个或多个item附加到一个数组的尾部 38 // 不像concat方法那样,它会修改数组array,如果item是一个数组, 39 // 它会将参数数组作为单个元素整个添加到数组中。它返回这个数组array的新长度值 40 var a = ['a', 'b', 'c']; 41 var b = ['x', 'y', 'z']; 42 var c1 = a.push(b, true); // a,b,c, [x,y,z], true 43 var c2 = a.push(b, false); // a,b,c, [x,y,z], true, [x,y,z], false 44 console.log("a: " + a); 45 console.log("c1: " + c1); 46 console.log("c2: " + c2); 47 console.log("----------------4 array.push(item...) end----------------"); 48 49 // array.reverse() 50 // 反转array中的元素的顺序 51 var a = ['a', 'b', 'c']; 52 var b = a.reverse(); 53 console.log("a: " + a); 54 console.log("b: " + b); 55 console.log("----------------5 array.reverse() end----------------"); 56 57 // array.shift() 58 // shift方法移除数组array中的第一个元素并返回该元素 59 var a = ['a', 'b', 'c']; 60 var b = a.shift(); 61 console.log("a: " + a); 62 console.log("b: " + b); 63 console.log("----------------6 array.shift() end----------------"); 64 65 // array.slice(start, end),前开后闭,end参数是可选的 66 // slice方法对array中的一段做浅复制 67 // 如果start大于等于array.length,得到的结果将是一个空数组 68 var a = ['a', 'b', 'c']; 69 var b = a.slice(0, 1); // 'a' 70 var c = a.slice(1); // 'b', 'c' 71 var d = a.slice(1, 2); // 'b' 72 console.log("a: " + a); 73 console.log("b: " + b); 74 console.log("c: " + c); 75 console.log("d: " + d); 76 console.log("----------------7 array.slice() end----------------"); 77 78 // array.spice(start, deleteCount, item...) 79 // splice方法从array中移除1个或多个元素,并用新的item替换它们, 80 // deleteCount是要移除的元素个数,它返回一个包含被移除元素的数组 81 var a = ['a', 'b', 'c']; 82 var b = a.splice(1, 1, 'ache', 'bug'); 83 console.log("a: " + a); // 'a', 'ache', 'bug', 'c' 84 console.log("b: " + b); // 'b' 85 console.log("----------------8 array.spice() end----------------"); 86 87 // array.sort(comparefn) 88 // sort方法对array中的内容进行适当的排序。 89 // 它不能正确地给一组数字排序 90 var a = ['a', 'c', 'b']; 91 var n = [4, 5, 15, 16, 23, 42]; 92 a.sort(); 93 n.sort(); 94 console.log("a: " + a); // a, b, c 95 console.log("n: " + n); // 15, 16, 23, 4, 42, 5 96 console.log("----------------9 array.sort(comparefn) end----------------"); 97 98 // array.unshift(item...) 99 // unshift方法像push方法一样用于将元素添加到数组中 100 // 但它是把item插入到array的开始部分而不是尾部。 101 // 它返回array的新的长度值 102 var a = ['a', 'b', 'c']; 103 var b = ['x', 'y', 'z']; 104 var c1 = a.push(b); 105 console.log("a: " + a); // [a,b,c], x, y, z 106 console.log("c1: " + c1); // 4 107 console.log("----------------10 array.unshift(item...) end----------------");
输出结果:
1 d1: a,b,c,x,y,z,true 2 d2: a,b,c,x,y,z,false 3 d3: a,b,c,x,y,z,1,2,3,true 4 ----------------1 array.concat(item...) end---------------- 5 a: a,b,c,d 6 c: abcd 7 ----------------2 array.join(separator) end---------------- 8 a: a,b 9 c: c 10 ----------------3 array.pop() end---------------- 11 a: a,b,c,x,y,z,true,x,y,z,false 12 c1: 5 13 c2: 7 14 ----------------4 array.push(item...) end---------------- 15 a: c,b,a 16 b: c,b,a 17 ----------------5 array.reverse() end---------------- 18 a: b,c 19 b: a 20 ----------------6 array.shift() end---------------- 21 a: a,b,c 22 b: a 23 c: b,c 24 d: b 25 ----------------7 array.slice() end---------------- 26 a: a,ache,bug,c 27 b: b 28 ----------------8 array.spice() end---------------- 29 a: a,b,c 30 n: 15,16,23,4,42,5 31 ----------------9 array.sort(comparefn) end---------------- 32 a: a,b,c,x,y,z 33 c1: 4 34 ----------------10 array.unshift(item...) end----------------
2、 Function
apply方法调用函数function, 传递一个将被绑定到this上的对象和一个可选的参数数组。apply方法被用在apply调用模式(apply invocation pattern)中。
3、 Number
1 // number.toExponential(fractionDigits) 2 // toExponential方法把这个number转换成一个指数形式的字符串 3 // 可选参数fractionDigits控制其小数点后的数字位数。它的值必须在0~2之间 4 console.log("Math.PI.toExponential(0): " + Math.PI.toExponential(0)); 5 console.log("Math.PI.toExponential(2): " + Math.PI.toExponential(2)); 6 console.log("Math.PI.toExponential(7): " + Math.PI.toExponential(7)); 7 console.log("Math.PI.toExponential(16): " + Math.PI.toExponential(16)); 8 console.log("Math.PI.toExponential(): " + Math.PI.toExponential()); 9 console.log("----------------1 number.toExponential(fractionDigits) end----------------"); 10 11 // number.toFixed(fractionDigits) 12 // toFixed方法把这个number转换成一个十进制数形式的字符串 13 // 可选参数fractionDigits控制其小数点后的数字位数。它的值必须在0~20之间,默认值为0 14 console.log("Math.PI.toFixed(0): " + Math.PI.toFixed(0)); 15 console.log("Math.PI.toFixed(2): " + Math.PI.toFixed(2)); 16 console.log("Math.PI.toFixed(7): " + Math.PI.toFixed(7)); 17 console.log("Math.PI.toFixed(16): " + Math.PI.toFixed(16)); 18 console.log("Math.PI.toFixed(): " + Math.PI.toFixed()); 19 console.log("----------------2 number.toFixed(fractionDigits) end----------------"); 20 21 // number.toPrecision(precision) 22 // toFixed方法把这个number转换成一个十进制数形式的字符串 23 // 可选参数precision控制有效数字的位数。它的值必须在1~21之间。 24 console.log("Math.PI.toPrecision(1): " + Math.PI.toPrecision(1)); 25 console.log("Math.PI.toPrecision(2): " + Math.PI.toPrecision(2)); 26 console.log("Math.PI.toPrecision(7): " + Math.PI.toPrecision(7)); 27 console.log("Math.PI.toPrecision(16): " + Math.PI.toPrecision(16)); 28 console.log("Math.PI.toPrecision(): " + Math.PI.toPrecision()); 29 console.log("----------------3 number.toPrecision(precision) end----------------"); 30 31 // number.toString(radix) 32 // toString方法把这个number转换成字符串 33 // 可选参数radix控制基数。它的值必须在2~36之间。默认的radix是以10为基数的。 34 // radix最常用的是整数,但是它可以用任意的数字 35 // 在最普通情况下,number.toString()可以更简单的写成String(number) 36 console.log("Math.PI.toString(2): " + Math.PI.toString(2)); 37 console.log("Math.PI.toString(8): " + Math.PI.toString(8)); 38 console.log("Math.PI.toString(16): " + Math.PI.toString(16)); 39 console.log("Math.PI.toString(): " + Math.PI.toString()); 40 console.log("String(Math.PI): " + String(Math.PI)); 41 console.log("----------------4 number.toString(radix) end----------------");
输出结果:
1 Math.PI.toExponential(0): 3e+0 2 Math.PI.toExponential(2): 3.14e+0 3 Math.PI.toExponential(7): 3.1415927e+0 4 Math.PI.toExponential(16): 3.1415926535897931e+0 5 Math.PI.toExponential(): 3.141592653589793e+0 6 ----------------1 number.toExponential(fractionDigits) end---------------- 7 Math.PI.toFixed(0): 3 8 Math.PI.toFixed(2): 3.14 9 Math.PI.toFixed(7): 3.1415927 10 Math.PI.toFixed(16): 3.1415926535897931 11 Math.PI.toFixed(): 3 12 ----------------2 number.toFixed(fractionDigits) end---------------- 13 Math.PI.toPrecision(1): 3 14 Math.PI.toPrecision(2): 3.1 15 Math.PI.toPrecision(7): 3.141593 16 Math.PI.toPrecision(16): 3.141592653589793 17 Math.PI.toPrecision(): 3.141592653589793 18 ----------------3 number.toPrecision(precision) end---------------- 19 Math.PI.toString(2): 11.001001000011111101101010100010001000010110100011 20 Math.PI.toString(8): 3.1103755242102643 21 Math.PI.toString(16): 3.243f6a8885a3 22 Math.PI.toString(): 3.141592653589793 23 String(Math.PI): 3.141592653589793 24 ----------------4 number.toString(radix) end----------------