JavaScript語言精粹 --- 方法
方法Array、 Function 、Number 、Object 、 RegExp 、StringArray
/* array.concat(item...) 返回一個新數組,不修改原數組 */var a = ['a', 'b', 'c'];var b = ['x', 'y', 'z'];var c = a.concat(b, true);// c is ['a', 'b', 'c', 'x', 'y', 'z', true]/* array.join(separator) 構造一個字串,預設“separator”分隔字元為:“,” */var a = ['a', 'b', 'c'];a.push('d');var c = a.join(''); // c is 'abcd';/* array.pop( ) 移除array 中的最後一個元素並返回該元素 */var a = ['a', 'b', 'c'];var c = a.pop( ); // a is ['a', 'b'] & c is 'c'/* array.push(item...) 附近item到一個數組的尾部,返回這個新數組的長度 */var a = ['a', 'b', 'c'];var b = ['x', 'y', 'z'];var c = a.push(b, true);// a is ['a', 'b', 'c', ['x', 'y', 'z'], true]// c is 5;/* array.shift() 移除數組中的第一個元素並返回該元素 */var a = ['a', 'b', 'c'];var c = a.shift( ); // a is ['b', 'c'] & c is 'a'/* array.unshift(item...) 與push方法剛好相反,在ie6中傳回值是undefined */var a = ['a', 'b', 'c'];var r = a.unshift('?', '@');// a is ['?', '@', 'a', 'b', 'c']// r is 5/* array.reverse() 方法反轉array 中的元素的順序。返回當前的array */var a = ['a', 'b', 'c'];var b = a.reverse( );// both a and b are ['c', 'b', 'a']/* array.slice(start[, end] ) 複製部分數組, start, end都可以是負數 */var a = ['a', 'b', 'c'];var b = a.slice(0, 1); // b is ['a']var c = a.slice(1); // c is ['b', 'c']var d = a.slice(1, 2); // d is ['b']/* array.sort(comparefn ) 有待深入學習 */var n = [4, 8, 15, 16, 23, 42];n.sort( );// n is [15, 16, 23, 4, 42, 8]/* array.splice(start, deleteCount, item...) 對數組中start開始,移除deleteCount個元素,並用新的item替換它們 */var a = ['a', 'b', 'c'];var r = a.splice(1, 1, 'ache', 'bug');// a is ['a', 'ache', 'bug', 'c']// r is ['b']
Function
/* function.apply(thisArg, argArray ) apply方法調用函數functions,傳遞一個將被綁定到 this上的對象(thisArg)和一個可選的參數數組(argArray)。*/
Number
/* number.toString(radix ) 方法把number轉換成一個的字串, 選擇性參數radix 控制基數,2-36之間,預設為10 *//* number.toExponential(fractionDigits) 方法把number轉換成一個指數形式的字串 *//* number.toFixed(fractionDigits ) 把number轉換為一個十進位的字串,fractionDigits表示小數點後面個數0-20之間 */
Object
/* object.hasOwnProperty(name ) 檢測object 是否包含name的屬性, 返回true/false */
RegExp
/* regexp.exec(string ) */var text = '' + 'This is bold<\/b>!<\/p><\/body><\/html>';var tags = /[^<>]+|<(\/?)([A-Za-z]+)([^<>]*)>/g;var a, i;while ((a = tags.exec(text))) { for (i = 0; i < a.length; i += 1) { document.writeln(('// [' + i + '] ' + a[i]).entityify( )); } document.writeln( );}/* regexp.test(string ) test方法是使用Regex最簡單/快的方法。如果regexp 比配 string,返回true,否則false */var b = /&.+;/.test('frank & beans');// b is true
String
/* string.charAt(pos ) */var name = 'Curly';var initial = name.charAt(0); // initial is 'C'/* string.charCodeAt(pos ) */var name = 'Curly';var initial = name.charCodeAt(0); // initial is 67/* string.concat(string...) */var s = 'C'.concat('a', 't'); // s is 'Cat'/* string.indexOf(searchString, position ) */var text = 'Mississippi';var p = text.indexOf('ss'); // p is 2p = text.indexOf('ss', 3); // p is 5p = text.indexOf('ss', 6); // p is -1/* string.lastIndexOf(searchString, position ) */var text = 'Mississippi';var p = text.lastIndexOf('ss'); // p is 5p = text.lastIndexOf('ss', 3); // p is 2p = text.lastIndexOf('ss', 6); // p is 5/* string.localeCompare(that ) */var m = ['AAA', 'A', 'aa', 'a', 'Aa', 'aaa'];m.sort(function (a, b) { return a.localeCompare(b);});// m (in some locale) is ['a', 'A', 'aa', 'Aa', 'aaa', 'AAA']/* string.match(regexp ) *//* string.replace(searchValue, replaceValue ) 只替換前一個匹配值*/var result = "mother_in_law".replace('_', '-');/* string.search(regexp ) */var text = 'and in it he says "Any damn fool could';var pos = text.search(/["']/); // pos is 18/* string.slice(start, end ) 同 string.substring(start, end )*//* string.split(separator, limit ) */var digits = '0123456789';var a = digits.split('', 5); // a is ['0', '1', '2', '3', '456789']var text = 'last, first ,middle';var d = text.split(/\s*,\s*/); // d is ['last', 'first', 'middle' ]/* string.substring(start, end ) *//* string.toLocaleLowerCase( ) *//* string.toLocaleUpperCase( ) *//* string.toLowerCase( ) *//* string.toUpperCase( ) *//* String.fromCharCode(char...) */var a = String.fromCharCode(67, 97, 116);// a is 'Cat'