Prototype Array對(duì)象 學(xué)習(xí)
更新時(shí)間:2009年07月19日 01:26:53 作者:
這個(gè)對(duì)象擴(kuò)展了JS原生的Array對(duì)象,提供了一些基本的工具函數(shù),有些方法非常簡單,源碼里就不在注釋了。
復(fù)制代碼 代碼如下:
Array.from = $A;
(function() {
//Array原型的引用
var arrayProto = Array.prototype,
slice = arrayProto.slice,
//JS 1.6里面會(huì)有原生的forEach方法
_each = arrayProto.forEach; // use native browser JS 1.6 implementation if available
function each(iterator) {
for (var i = 0, length = this.length; i < length; i++)
iterator(this[i]);
}
//如果不是JS1.6,_each設(shè)置成對(duì)象的each方法
//這里的_each方法是覆蓋了Enuerable里面的_each方法
if (!_each) _each = each;
function clear() {
this.length = 0;
return this;
}
function first() {
return this[0];
}
function last() {
return this[this.length - 1];
}
//返回所有Array內(nèi)不為null的數(shù)據(jù)
function compact() {
return this.select(function(value) {
return value != null;
});
}
//把多維數(shù)組壓成一維數(shù)組
function flatten() {
return this.inject([], function(array, value) {
if (Object.isArray(value))
return array.concat(value.flatten()); //這里有遞歸調(diào)用
array.push(value);
return array;
});
}
function without() {
var values = slice.call(arguments, 0);
return this.select(function(value) {
return !values.include(value);
});
}
function reverse(inline) {
return (inline !== false ? this : this.toArray())._reverse();
}
//返回所有Array內(nèi)不重復(fù)的元素,如果數(shù)組是有序的,傳入true參數(shù),執(zhí)行起來會(huì)更快
function uniq(sorted) {
return this.inject([], function(array, value, index) {
if (0 == index || (sorted ? array.last() != value : !array.include(value)))
array.push(value);
return array;
});
}
//取兩個(gè)數(shù)組的交集
function intersect(array) {
return this.uniq().findAll(function(item) {
return array.detect(function(value) { return item === value });
});
}
function clone() {
return slice.call(this, 0);
}
function size() {
return this.length;
}
function inspect() {
return '[' + this.map(Object.inspect).join(', ') + ']';
}
function toJSON() {
var results = [];
this.each(function(object) {
var value = Object.toJSON(object);
if (!Object.isUndefined(value)) results.push(value);
});
return '[' + results.join(', ') + ']';
}
function indexOf(item, i) {
i || (i = 0);
var length = this.length;
if (i < 0) i = length + i;
for (; i < length; i++)
if (this[i] === item) return i;
return -1;
}
function lastIndexOf(item, i) {
i = isNaN(i) ? this.length : (i < 0 ? this.length + i : i) + 1;
var n = this.slice(0, i).reverse().indexOf(item);
return (n < 0) ? n : i - n - 1;
}
function concat() {
var array = slice.call(this, 0), item;
for (var i = 0, length = arguments.length; i < length; i++) {
item = arguments[i];
//這的第二條件是防止把調(diào)用方法的數(shù)組元素也一起concat起來
if (Object.isArray(item) && !('callee' in item)) {
for (var j = 0, arrayLength = item.length; j < arrayLength; j++)
array.push(item[j]);
} else {
array.push(item);
}
}
return array;
}
//mixin Enumerable里面的方法
Object.extend(arrayProto, Enumerable);
if (!arrayProto._reverse)
arrayProto._reverse = arrayProto.reverse;
Object.extend(arrayProto, {
_each: _each,
clear: clear,
first: first,
last: last,
compact: compact,
flatten: flatten,
without: without,
reverse: reverse,
uniq: uniq,
intersect: intersect,
clone: clone,
toArray: clone,
size: size,
inspect: inspect,
toJSON: toJSON
});
//這個(gè)bug網(wǎng)上沒搜到,誰知道說一下?
var CONCAT_ARGUMENTS_BUGGY = (function() {
return [].concat(arguments)[0][0] !== 1;
})(1,2)
if (CONCAT_ARGUMENTS_BUGGY) arrayProto.concat = concat;
//檢查JS是否原生支持indexOf和lastIndexOf方法,不支持則設(shè)置成對(duì)象內(nèi)的方法
if (!arrayProto.indexOf) arrayProto.indexOf = indexOf;
if (!arrayProto.lastIndexOf) arrayProto.lastIndexOf = lastIndexOf;
})();
clear
clone
compact
each
first
flatten
from
indexOf
inspect
last
reduce
reverse
size
toArray
toJSON
uniq
without
下面給出一些方法的例子,簡單的方法就不給出例子了。
flatten():
復(fù)制代碼 代碼如下:
['frank', ['bob', 'lisa'], ['jill', ['tom', 'sally']]].flatten()
// -> ['frank', 'bob', 'lisa', 'jill', 'tom', 'sally']
reduce():這個(gè)方法的意思就是,如果數(shù)字里面有一個(gè)數(shù)據(jù),這直接返回這個(gè)數(shù)據(jù),否則返回原來的數(shù)組
uniq():
復(fù)制代碼 代碼如下:
['Sam', 'Justin', 'Andrew', 'Dan', 'Sam'].uniq();
// -> ['Sam', 'Justin', 'Andrew', 'Dan']
['Prototype', 'prototype'].uniq();
// -> ['Prototype', 'prototype'] because String comparison is case-sensitive
without():
復(fù)制代碼 代碼如下:
[3, 5, 6, 1, 20].without(3)
// -> [5, 6, 1, 20]
[3, 5, 6, 1, 20].without(20, 6)
// -> [3, 5, 1]
相關(guān)文章
prototype 1.5相關(guān)知識(shí)及他人筆記
prototype 1.5相關(guān)知識(shí)及他人筆記...2006-12-12
prototype Element學(xué)習(xí)筆記(Element篇三)
上一篇把Element的所函數(shù)都梳理了一遍,下面總結(jié)一下這些函數(shù)的功能,畢竟函數(shù)太多,不分門別類一下還是沒有底。2008-10-10
Prototype Hash對(duì)象 學(xué)習(xí)
這個(gè)對(duì)象相當(dāng)于Java中的HashMap,當(dāng)然了功能沒HashMap那么強(qiáng)大。提供一直基本的方法,簡單的方法就不在源碼中注釋了。2009-07-07
Prototype RegExp對(duì)象 學(xué)習(xí)
幫助文檔上沒有這個(gè)對(duì)象,實(shí)際上源代碼中這個(gè)對(duì)象還是有方法的,就1靜態(tài)方法,作用也不是很大,這里簡單說一下,因?yàn)橐院蠼榻B別的對(duì)象時(shí)會(huì)用到這個(gè)RegExp2009-07-07
Prototype Function對(duì)象 學(xué)習(xí)
這個(gè)對(duì)象就是對(duì)function的一些擴(kuò)充,最重要的當(dāng)屬bind方法,其中wrap方法也很重要,在類繼承機(jī)制里面就是利用wrap方法來調(diào)用父類的同名方法。2009-07-07
初學(xué)prototype,發(fā)個(gè)JS接受URL參數(shù)的代碼
初學(xué)prototype,發(fā)個(gè)JS接受URL參數(shù)的代碼...2007-01-01
prototype Element學(xué)習(xí)筆記(篇一)
Element,哈哈哈。遇到正主了,到現(xiàn)在為止才遇到讓我高興的玩意。當(dāng)初Ext.Element可是花三千余行代碼專門來封裝啊。我倒要看一看它的代碼了。事實(shí)上prototype中我最想研究的只有兩個(gè)內(nèi)容:Element、Selector。這兩個(gè)東西是精華。2008-10-10
Prototype Date對(duì)象 學(xué)習(xí)
這個(gè)對(duì)象里面就一個(gè)toJSON方法,非常簡單2009-07-07

