Microsoft AJAX Library Cheat Sheet(1): Array类型的扩展
本文为翻译,英文原版的Cheat Sheet(PDF版本)在此下载:http://aspnetresources.com/downloads/ms_ajax_library_cheat_sheets1.zip
原作版权声明:
Copyright (c) 2004-2006, Milan Negovan http://www.AspNetResources.com All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
注:标注有[S]的为静态方法,无须实例化对象即可使用。
[S] Array.add (array, item)
添加一项至Array的结尾。
var a = ['a','b','c','d'];
Array.add(a, 'e');
// a = ['a','b','c','d','e']
[S] Array.addRange (array, items)
将items中的项依次添加到Array的结尾。
var a = ['a', 'b', 'c', 'd', 'e'];
var b = ['f', 'g','h'];
Array.addRange(a, b);
// a = ['a','b','c','d','e','f','g','h']
[S] Array.clear (array)
清除该Array中的所有项。
[S] Array.clone (array)
返回该Array的浅拷贝(Shallow Copy)。
注意:浅拷贝只包含原Array中的项,无论是引用类型还是值类型。但浅拷贝并不会复制被引用的对象,新的Array和原Array中的某个元素引用的是同一个对象的实例。
var a = ['a','b','c','d'];
var b = Array.clone(a);
// b = ['a','b','c','d']
[S] Array.contains (array, item)
判断指定的项是否包含在该Array中。
var a = ['red','green','blue','yellow'];
var b = Array.contains(a, "red");
// b = true
[S] Array.dequeue (array)
从原Array中删除第一项,并返回该项。
var myArray = [],result = "";
Array.add(myArray, 'a');
Array.add(myArray, 'b');
Array.add(myArray, 'c');
Array.add(myArray, 'd');
result = Array.dequeue(myArray);
// myArray = ['b','c', 'd'], result = 'a'
[S] Array.forEach (array, method, context)
依次访问该Array中的每一项,但跳过值为undefined的项。
var a = ['a', 'b', 'c', 'd'];
a[5] = 'e';
var result = '';
function appendToString(arrayElement, index, array) {
// "this" is the context parameter, i.e. '|'.
result += arrayElement + this + index + ',';
}
Array.forEach(a, appendToString, '|');
// result = a|0,b|1,c|2,d|3,e|4,
[S] Array.indexOf (array, item, start)
在Array中搜索某一项,并返回其在该Array中的索引。若该Array中不包含该项,则返回-1。
var a = ['red', 'blue', 'green', 'blue'];
var myFirstIndex = Array.indexOf(a, "blue");
var mySecondIndex = Array.indexOf (a, "blue", (myFirstIndex + 1));
// myFirstIndex = 1, mySecondIndex = 3
[S] Array.insert (array, index, item)
在Array中的某一位置插入新项。
var a = ['a', 'b', 'd', 'e'];
Array.insert(a, 2, 'c');
// a = ['a','b','c','d','e']
[S] Array.parse (value)
从字符串表达方式解析出真正的Array对象。
var a = Array.parse ("['red', 'blue', 'green']");
// a[0] = 'red', a[1] = 'blue', a[2] = 'green'
[S] Array.enqueue (array, item)
添加一项至Array的结尾。请同时参考Array.dequeue (array)。
[S] Array.remove(array, item)
从该Array中移除(第一次出现的)该项。
var a = ['a', 'b', 'c', 'd', 'e'];
Array.remove(a, 'c');
// a = ['a','b','d','e']
Array.removeAt(a, 2);
// a = ['a','b','e']
[S] Array.removeAt(array, index)
从该Array中移除指定索引位置上的项。