(function() { //Array原型的引用 var arrayProto = Array.prototype, slice = arrayProto.slice, //JS 1.6里面會有原生的forEach方法 _each = arrayProto.forEach; " /> 久久精品麻豆国产天美传媒果冻,人妻免费视频公开上传,亚洲国产在线精品国偷产拍

天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

Prototype Array對象 學習

復制代碼 代碼如下:
Array.from = $A;

(function() {
//Array原型的引用    
var arrayProto = Array.prototype,
slice = arrayProto.slice,
     //JS 1.6里面會有原生的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設置成對象的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內不為null的數據
function compact() {
return this.select(function(value) {
return value != null;
});
}

//把多維數組壓成一維數組
function flatten() {
return this.inject([], function(array, value) {
if (Object.isArray(value))
return array.concat(value.flatten()); //這里有遞歸調用
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內不重復的元素,如果數組是有序的,傳入true參數,執行起來會更快
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;
});
}

//取兩個數組的交集
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];
     //這的第二條件是防止把調用方法的數組元素也一起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
});

//這個bug網上沒搜到,誰知道說一下?
var CONCAT_ARGUMENTS_BUGGY = (function() {
return [].concat(arguments)[0][0] !== 1;
})(1,2)

if (CONCAT_ARGUMENTS_BUGGY) arrayProto.concat = concat;

//檢查JS是否原生支持indexOf和lastIndexOf方法,不支持則設置成對象內的方法
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():
復制代碼 代碼如下:
['frank', ['bob', 'lisa'], ['jill', ['tom', 'sally']]].flatten()
// -> ['frank', 'bob', 'lisa', 'jill', 'tom', 'sally']


reduce():這個方法的意思就是,如果數字里面有一個數據,這直接返回這個數據,否則返回原來的數組
uniq():
復制代碼 代碼如下:
['Sam', 'Justin', 'Andrew', 'Dan', 'Sam'].uniq();
// -> ['Sam', 'Justin', 'Andrew', 'Dan']

['Prototype', 'prototype'].uniq();
// -> ['Prototype', 'prototype'] because String comparison is case-sensitive


without():
復制代碼 代碼如下:
[3, 5, 6, 1, 20].without(3)
// -> [5, 6, 1, 20]

[3, 5, 6, 1, 20].without(20, 6)
// -> [3, 5, 1]

JavaScript技術Prototype Array對象 學習,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 色窝窝777欧美午夜精品影院 | 一级毛片美国 | yellow免费| 国产亚洲999精品AA片在线爽 | TUBE19UP老师学生 | 亚洲欧美在无码片一区二区 | 偷拍亚洲制服另类无码专区 | 日日噜噜夜夜爽爽 | 顶级少妇AAAAABBBBB片 | 花蝴蝶在线观看免费中文版高清 | 亚洲精品久久YY5099 | 忘忧草研究所 麻豆 | 国产精品久久久久久久久99热 | chinese学生gv video| 国产欧美国产综合第一区 | 亚洲欧美日韩另类精品一区二区三区 | 大地影院日本韩国电影免费观看 | 国产在线观看的 | 久久精品九九亚洲精品天堂 | 欧美肥胖女人bbwbbw视频 | 久久精品无码一区二区日韩av | 饥渴的新婚女教师 | 亚洲国产精品一区二区久久第 | 国产午夜永久福利视频在线观看 | 国精产品一区一区三区M | 99视频这里只有精品 | 国产AV99激情久久无码天堂 | 婷婷五月久久精品国产亚洲 | 动漫在线观看免费肉肉 | 丝瓜视频樱桃视频在线观看免费 | 性夜夜春夜夜爽AA片A | 寻找最美乡村教师颁奖晚会 | 午夜深情在线观看免费 | 国产精品久久久久永久免费看 | 国产人妻麻豆蜜桃色在线 | 扒开女人下面使劲桶动态图 | 久久久久综合一本久道 | 亚洲精品国偷拍自产在线观看蜜臀 | 亚洲一区自拍高清亚洲精品 | 879影视动漫h免费观看 | 免费国产成人高清在线观看视频 |