Javascript数组常用艺术[包含MS AJAX.NET的prototype扩展方法]示例

扣押了JefferyZhao的MSDN web cast视频教程,亲自实施了转,代码如下:

Ajax 1Ajax 2<%Ajax 3@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”Default.aspx.cs” Inherits=”_Default” %>
Ajax 4<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN” “http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"&gt;
Ajax 5<html xmlns=”http://www.w3.org/1999/xhtml"&gt;
Ajax 6<head runat=”server”>
Ajax 7    <title>Javascript Array常用方法言传身教</title>
Ajax 8</head>
Ajax 9<body>
Ajax 10    <form id=”form1″ runat=”server”>
Ajax 11        <asp:ScriptManager ID=”ScriptManager1″ runat=”server” />
Ajax 12        <div id=”_display”>
Ajax 13        </div>
Ajax 14        
Ajax 15Ajax 16        <script type=”text/javascript”>Ajax 17
Ajax 18            function display(str)
Ajax 19Ajax 20            Ajax 21{
Ajax 22                $get(“_display”).innerHTML += str + “<br/>”;
Ajax 23            }            
Ajax 24            
Ajax 25Ajax 26            Array.prototype.display = function()Ajax 27{
Ajax 28                display(“  a: ” + this.toString());
Ajax 29            }
Ajax 30            
Ajax 31            var a = [1,2,3,4,5,6,7,8];
Ajax 32            
Ajax 33            display(“a.toString()”);
Ajax 34            a.display();//1,2,3,4,5,6,7,8
Ajax 35            
Ajax 36            display(“a.push(9)”);
Ajax 37            a.push(9);
Ajax 38            a.display();//1,2,3,4,5,6,7,8,9          
Ajax 39            
Ajax 40            display(“a.shift()”);
Ajax 41            a.shift();
Ajax 42            a.display();//2,3,4,5,6,7,8,9 利用push和shift可以学一个queue
Ajax 43            
Ajax 44            
Ajax 45            display(“a.unshift(1)”);
Ajax 46            a.unshift(1);
Ajax 47            a.display();//0,2,3,4,5,6,7,8,9
Ajax 48            
Ajax 49            display(“a.pop()”);
Ajax 50            a.pop();
Ajax 51            a.display();//1,2,3,4,5,6,7,8
Ajax 52            
Ajax 53            display(“a.slice(2,5) = ” + a.slice(2,5));//3,4,5 取下标2到5间的元素(包含下标2)
Ajax 54            a.display()
Ajax 55            
Ajax 56            display(“a.slice(2,-2) = ” + a.slice(2,-2));//3,4,5,6 取下标2到倒数第二个要素中的所有因素(包含下标2)
Ajax 57            a.display();
Ajax 58            
Ajax 59            display(“a.concat(‘a’,’b’) = ” +  a.concat(‘a’,’b’));//1,2,3,4,5,6,7,8,a,b
Ajax 60            a.display();
Ajax 61            
Ajax 62            display(“‘[‘ + a.join(‘][‘) + ‘]’ = ” + ‘[‘ + a.join(‘][‘) + ‘]’) ;//[1][2][3][4][5][6][7][8]
Ajax 63            a.display();
Ajax 64            
Ajax 65            display(“a.splice(3,2) = ” + a.splice(3,2));
Ajax 66            a.display();//1,2,3,6,7,8 从下标3开去了亚个因素
Ajax 67            
Ajax 68            display(“a.splice(3,0,4,5)”);
Ajax 69            a.splice(3,0,4,5);
Ajax 70            a.display();//1,2,3,4,5,6,7,8 从下标3初始,删除0单要素,再插入4,5眼看第二只元素,呵呵,同一个智,即能去,又能插入,是匪是发出硌意外          
Ajax 71          
Ajax 72            display(“a.splice(3,2,’a’,’b’,’c’,9,10)”);
Ajax 73            a.splice(3,2,’a’,’b’,’c’,9,10);
Ajax 74            a.display();//1,2,3,a,b,c,9,10,6,7,8 从下标3开,删除2只要素,再插入a,b,c,9,10马上三个因素,相当给替换元素
Ajax 75            
Ajax 76            display(“a.reverse()”);
Ajax 77            a.reverse();//8,7,6,10,9,c,b,a,3,2,1
Ajax 78            a.display();
Ajax 79            
Ajax 80            display(“a.sort()”);
Ajax 81            a.sort();
Ajax 82            a.display();//1,10,2,3,6,7,8,9,a,b,c 注意,这里默认是按部就班字符串来排序的
Ajax 83            
Ajax 84            display(“a = [1,2,3,4,5,6,7,8,9,10,11,12];”)
Ajax 85            a = [1,2,3,4,5,6,7,8,9,10,11,12];
Ajax 86            a.display();
Ajax 87            display(“a.sort(function(x,y){return y-x});”)
Ajax 88Ajax 89            a.sort(function(x,y)Ajax 90{return y-x});//倒序排列
Ajax 91            a.display();//12,11,10,9,8,7,6,5,4,3,2,1 
Ajax 92            
Ajax 93            display(“<hr/>以下方式是MS Ajax.Net扩展Array的Prototype得到的初章程<hr/>”);
Ajax 94            
Ajax 95            display(“Array.enqueue(a,\”a\”)”)
Ajax 96            Array.enqueue(a,”a”);//入队列
Ajax 97            a.display();//12,11,10,9,8,7,6,5,4,3,2,1,a 
Ajax 98            
Ajax 99            display(“Array.dequeue(a)”)
Ajax 100            Array.dequeue(a);//出队列
Ajax 101            a.display();//11,10,9,8,7,6,5,4,3,2,1,a
Ajax 102            
Ajax 103            var b = [‘hello’,’world’];
Ajax 104            display(“var b = [‘hello’,’world’];”);
Ajax 105            
Ajax 106            display(“Array.addRange(a,b)”)
Ajax 107            Array.addRange(a,b);
Ajax 108            a.display();
Ajax 109            
Ajax 110            display(“Array.contains(a,’hello’)”);
Ajax 111            display(Array.contains(a,’hello’));//true,a数组中是否带有元素hello
Ajax 112            
Ajax 113            display(“Array.insert(a,0,’hi’)”);
Ajax 114            Array.insert(a,0,’hi’);//在生标0处插入元素hi
Ajax 115            a.display();//hi,11,10,9,8,7,6,5,4,3,2,1,a,hello,world
Ajax 116            
Ajax 117            display(“Array.remove(a,\”hello\”)”)
Ajax 118            Array.remove(a,”hello”);
Ajax 119            a.display();
Ajax 120            
Ajax 121            display(“Array.removeAt(0)”);
Ajax 122            Array.removeAt(a,0);//删除下标0的因素
Ajax 123            a.display();//11,10,9,8,7,6,5,4,3,2,1,a,world
Ajax 124            
Ajax 125            display(“var c = Array.clone(a);”) ;
Ajax 126            var c = Array.clone(a);
Ajax 127            display(“c=” + c);
Ajax 128            
Ajax 129            display(“var d = Array.parse(\”[1,2,3,4,5]\”)”)
Ajax 130            var d = Array.parse(“[1,2,3,4,5]”);
Ajax 131            display(“d.length=” + d.length);
Ajax 132            
Ajax 133            display(“Array.indexOf(a,’world’)=” + Array.indexOf(a,’world’));
Ajax 134            
Ajax 135            
Ajax 136            display(“Array.add(a,\”X-man\”)”)
Ajax 137            Array.add(a,”X-man”);//效果等同于enqueue方法
Ajax 138            a.display();//11,10,9,8,7,6,5,4,3,2,1,a,world,X-man
Ajax 139            
Ajax 140            
Ajax 141Ajax 142            var obj = Ajax 143{result:””};
Ajax 144            
Ajax 145            function newMethod(item,index,array)
Ajax 146Ajax 147            Ajax 148{
Ajax 149                this.result += “[” + index + “:” + item + “]”;//将数组每个元素构成成“[下标:值]”的字符串返回给obj.result
Ajax 150                array[index] = ‘x’ + array[index];//将数组每个元素前加一个’x’
Ajax 151            }
Ajax 152            
Ajax 153            Array.forEach(a,newMethod,obj);          
Ajax 154            display(“Array.forEach(a,newMethod,obj)”);
Ajax 155            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]
Ajax 156            display(a);//x11,x10,x9,x8,x7,x6,x5,x4,x3,x2,x1,xa,xworld,xX-man
Ajax 157            
Ajax 158            display(“Array.clear(a)”);
Ajax 159            Array.clear(a);
Ajax 160            a.display();
Ajax 161            
Ajax 162            
Ajax 163
Ajax 164         
Ajax 165            
Ajax 166        </script>
Ajax 167    </form>
Ajax 168</body>
Ajax 169</html>
Ajax 170

相关文章