<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <script> function add(a,b){ return Number(a) + Number(b); }; var arr = [1,2,3,4,5,6,7,8,9,10]; Array.prototype.reduce_self = function(f,value){ var i; for(i = 0; i<this.length; i+=1){ value = f( this[i] , value ) }; return value; }; Array.prototype.dim = function(n,init){ var a = [] , i; for(i = 0; i < n; i+=1){ a[i] = init }; return a; }; Array.prototype.matrix = function(m,n,initial){ var a, i, j, mat = []; for(i=0; i<m; i+=1){ a = []; for(j=0; j<n; j++){ a[j] = initial; }; mat[i] = a; } return mat; }; //RegExp var parse_url = /^(?:([a-zA-z]+):)?(\/{0,3})([0-9.\-a-zA-Z]+)(?::(\d+))?/; var url = "http://www.chenqihao.com:80/goodparts?q#fragment"; var result = parse_url.exec(url); var names = ['url','scheme','slash','host','port']; var blanks = ' ', i ; for(i=0; i<names.length; i+=1){ document.writeln(names[i] + ':' + result[i] + '<br>') }; //排序 var arr = [2,1,3,66,10,200,28,29]; arr.sort(); console.log(arr) arr.sort(function(a,b){ if(a<b){ return 1 }else{ return -1 } }); console.log(arr); var arr1 = [10,29,'ddd',100,'bbd',29,0,18,'a',18,'b']; arr1.sort(function(a,b){ if( a === b){ return 0; } if( typeof a == 'number' && typeof b == 'number' ){ if(a < b){return -1}else{ return 1}; }; if( typeof a < typeof b){ return -1 }; }); var by = function(name){ return function(a,b){ var c,d; if(typeof a === 'object' && typeof b === 'object' && a && b){ c = a [name],d = b[name]; if(a === b){ return 0 }; if( typeof a === typeof b ){ return a < b ? -1 : 1; }; return typeof a < typeof b ? -1 : 1; }; }; }; var obj = [ {f : 'C'}, {f : 'E'}, {f : 'B'}, {f : 'A'}, {f : 'S'} ]; obj.sort( by('f') ); console.log( JSON.stringify(obj) ); var arr = ['a','b','c','d']; arr._unshift = function(){ this.splice.apply(this,[0,0].concat(Array.prototype.slice.call(arguments))) }; //Array.prototype.slice.call(arguments) 可以将类数组转换为真数组 Function.prototype._bind = function(that){ var method = this, slice = Array.prototype.slice, args = slice.apply(arguments,[1]); return function(){ method.apply( that,args.concat(slice.apply(arguments,[0])) ) }; }; //下面这个东东,经常服务器返回东西经过编码了,用这个在转下;反正转来转去的 /* 主要是这四个字符 < > & " */ String.prototype.entityfy = function(){ var charactor = { '<' : '<', '>' : '>', '&' : '&', '"' : '"' }; var _this = this; return (function(){ return _this.replace(/[<>&"]/g,function(a){ return charactor[a] }) })(); }; console.log( '<>&"'.entityfy() ) </script> </body> </html>