看了JefferyZhao的MSDN web cast视频教程,亲自实践了一下,代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Javascript Array常用方法示例</title> </head> <body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server" /> <div id="_display"> </div> <script type="text/javascript"> function display(str) { $get("_display").innerHTML += str + "<br/>"; } Array.prototype.display = function(){ display(" a: " + this.toString()); } var a = [1,2,3,4,5,6,7,8]; display("a.toString()"); a.display();//1,2,3,4,5,6,7,8 display("a.push(9)"); a.push(9); a.display();//1,2,3,4,5,6,7,8,9 display("a.shift()"); a.shift(); a.display();//2,3,4,5,6,7,8,9 利用push和shift可以模拟一个queue display("a.unshift(1)"); a.unshift(1); a.display();//0,2,3,4,5,6,7,8,9 display("a.pop()"); a.pop(); a.display();//1,2,3,4,5,6,7,8 display("a.slice(2,5) = " + a.slice(2,5));//3,4,5 取下标2到5之间的元素(包含下标2) a.display() display("a.slice(2,-2) = " + a.slice(2,-2));//3,4,5,6 取下标2到倒数第二个元素之间的所有元素(包含下标2) a.display(); display("a.concat('a','b') = " + a.concat('a','b'));//1,2,3,4,5,6,7,8,a,b a.display(); display("'[' + a.join('][') + ']' = " + '[' + a.join('][') + ']') ;//[1][2][3][4][5][6][7][8] a.display(); display("a.splice(3,2) = " + a.splice(3,2)); a.display();//1,2,3,6,7,8 从下标3开始删除了二个元素 display("a.splice(3,0,4,5)"); a.splice(3,0,4,5); a.display();//1,2,3,4,5,6,7,8 从下标3开始,删除0个元素,再插入4,5这二个元素,呵呵,同一个方法,即能删除,又能插入,是不是有点意外 display("a.splice(3,2,'a','b','c',9,10)"); a.splice(3,2,'a','b','c',9,10); a.display();//1,2,3,a,b,c,9,10,6,7,8 从下标3开始,删除2个元素,再插入a,b,c,9,10这三个元素,相当于替换元素 display("a.reverse()"); a.reverse();//8,7,6,10,9,c,b,a,3,2,1 a.display(); display("a.sort()"); a.sort(); a.display();//1,10,2,3,6,7,8,9,a,b,c 注意,这里默认是按字符串来排序的 display("a = [1,2,3,4,5,6,7,8,9,10,11,12];") a = [1,2,3,4,5,6,7,8,9,10,11,12]; a.display(); display("a.sort(function(x,y){return y-x});") a.sort(function(x,y){return y-x});//倒序排列 a.display();//12,11,10,9,8,7,6,5,4,3,2,1 display("<hr/>以下方法是MS Ajax.Net扩展Array的Prototype得到的新方法<hr/>"); display("Array.enqueue(a,\"a\")") Array.enqueue(a,"a");//入队列 a.display();//12,11,10,9,8,7,6,5,4,3,2,1,a display("Array.dequeue(a)") Array.dequeue(a);//出队列 a.display();//11,10,9,8,7,6,5,4,3,2,1,a var b = ['hello','world']; display("var b = ['hello','world'];"); display("Array.addRange(a,b)") Array.addRange(a,b); a.display(); display("Array.contains(a,'hello')"); display(Array.contains(a,'hello'));//true,a数组中是否包含元素hello display("Array.insert(a,0,'hi')"); Array.insert(a,0,'hi');//在下标0处插入元素hi a.display();//hi,11,10,9,8,7,6,5,4,3,2,1,a,hello,world display("Array.remove(a,\"hello\")") Array.remove(a,"hello"); a.display(); display("Array.removeAt(0)"); Array.removeAt(a,0);//删除下标0的元素 a.display();//11,10,9,8,7,6,5,4,3,2,1,a,world display("var c = Array.clone(a);") ; var c = Array.clone(a); display("c=" + c); display("var d = Array.parse(\"[1,2,3,4,5]\")") var d = Array.parse("[1,2,3,4,5]"); display("d.length=" + d.length); display("Array.indexOf(a,'world')=" + Array.indexOf(a,'world')); display("Array.add(a,\"X-man\")") Array.add(a,"X-man");//效果等同于enqueue方法 a.display();//11,10,9,8,7,6,5,4,3,2,1,a,world,X-man var obj = {result:""}; function newMethod(item,index,array) { this.result += "[" + index + ":" + item + "]";//将数组每个元素组合成“[下标:值]”的字符串返回给obj.result array[index] = 'x' + array[index];//将数组每个元素前加一个'x' } Array.forEach(a,newMethod,obj); display("Array.forEach(a,newMethod,obj)"); display(obj.result);//[0:11][1:10][2:9][3:8][4:7][5:6][6:5][7:4][8:3][9:2][10:1][11:a][12:world][13:X-man] display(a);//x11,x10,x9,x8,x7,x6,x5,x4,x3,x2,x1,xa,xworld,xX-man display("Array.clear(a)"); Array.clear(a); a.display(); </script> </form> </body> </html>