Array方面Js底層代碼學習記錄
一、.clear() →Array
function clear() { this.length = 0; return this; }
返回清除item的空數組。
例子:
var fruits = [‘Apple‘, ‘Orange‘, ‘Bananas‘, ‘peach‘]; fruits.clear(); // -> [] fruits // -> []
二、.clone() →Array
function clone() {return slice.call(this, 0); }
返回數組的副本,保持原始數組不變。
三、.compact() →Array
function compact() { return this.select(function(value) { return value != null; }); }
返回一個副本,不包含null和undefined
例子:
var orig = [undefined, ‘A‘, undefined, ‘B‘, null, ‘C‘]; var copy = orig.compact();// orig -> [undefined, ‘A‘, undefined, ‘B‘, null, ‘C‘]; // copy -> [‘A‘, ‘B‘, ‘C‘];
四、Every([iterator = Prototype.K[, context]]) → Boolean
iterator
(Function
) - 一個可選函數,用於評估枚舉中的每個元素; 該函數應該返回值來測試。如果沒有提供,則測試元素本身。context
(Object
) -this
在對叠代器的調用中使用的可選對象。
確定是否所有元素都是真實的(boolean-equivalent true),直接或通過提供的叠代器計算。(這一塊我還不能很好理解)
function every(iterator) { if (this == null) throw new TypeError(); iterator = iterator || Prototype.K; var context = arguments[1]; var object = Object(this); for (var i = 0, length = object.length >>> 0; i < length; i++) { if (i in object && !iterator.call(context, object[i], i, object)) { return false; } } return true; } if (arrayProto.every) { every = wrapNative(Array.prototype.every); }
五、.filter(iterator[, context]) →Array
iterator
(Function
) - 用於測試元素的叠代器函數。context
(Object
) -this
在對叠代器的調用中使用的可選對象。
返回包含此數組中所有項目的新數組,其中 iterator
返回了一個真值。
function filter(iterator) { if (this == null || !Object.isFunction(iterator)) throw new TypeError(); var object = Object(this); var results = [], context = arguments[1], value; for (var i = 0, length = object.length >>> 0; i < length; i++) { if (i in object) { value = object[i]; if (iterator.call(context, value, i, object)) { results.push(value); } } } return results; } if (arrayProto.filter) { // `Array#filter` requires an iterator by nature, so we don‘t need to // wrap it. filter = Array.prototype.filter; }
六、.first()
function first() { return this[0]; }
返回數組的第一個項目(例如,array[0]
)。
七、.flatten() →Array
function flatten() { return this.inject([], function(array, value) { if (Object.isArray(value)) return array.concat(value.flatten()); array.push(value); return array; }); }
個人理解:合並指定數組內的所有數組。
官方理解:
返回數組的平坦(一維)副本,保持原始數組不變。嵌套數組以遞歸方式內聯註入。
常用在處理遞歸收集算法的結果。
例子:
var a = [ ‘ frank ‘,[ ‘ bob ‘,‘ lisa ‘ ],[ ‘ jill ‘,[ ‘ tom ‘,‘ sally ‘ ]]]; var b = a.flatten(); // a - > [‘frank‘,[‘bob‘,‘lisa‘],[‘jill‘,[‘tom‘,‘sally‘]]] // b - > [‘frank‘,‘bob‘, ‘lisa‘,‘jill‘,‘tom‘,‘sally‘]
八、.indexOf(item[, offser = 0]) →Number
function indexOf(item, i) { if (this == null) throw new TypeError(); var array = Object(this), length = array.length >>> 0; if (length === 0) return -1; // The rules for the `fromIndex` argument are tricky. Let‘s follow the // spec line-by-line. i = Number(i); if (isNaN(i)) { i = 0; } else if (i !== 0 && isFinite(i)) { // Equivalent to ES5‘s `ToInteger` operation. i = (i > 0 ? 1 : -1) * Math.floor(Math.abs(i)); } // If the search index is greater than the length of the array, // return -1. if (i > length) return -1; // If the search index is negative, take its absolute value, subtract it // from the length, and make that the new search index. If it‘s still // negative, make it 0. var k = i >= 0 ? i : Math.max(length - Math.abs(i), 0); for (; k < length; k++) if (k in array && array[k] === item) return k; return -1; }
官方理解:
item
(?
) - 可能存在或不存在於數組中的值。offset
(Number
) - 開始搜索前要跳過的初始項目數。
返回item數組中第一次出現的索引,或者-1如果item不存在於數組中。Array#indexOf使用絕對等於(===)比較項目。
個人理解:返回item在數組中首次出現的位置的索引,因為區分了使用了絕對等於所以區分大小寫,類型。
例子:
[3, 5, 6, 1, 20].indexOf(1) // -> 3 [3, 5, 6, 1, 20].indexOf(90) // -> -1 (not found) [‘1‘, ‘2‘, ‘3‘].indexOf(1); // -> -1 (not found, 1 !== ‘1‘)
加一個不是Array的例子:
var str="Hello world!" document.write(str.indexOf("Hello") + "<br />") document.write(str.indexOf("World") + "<br />") document.write(str.indexOf("world")) // ->0 // ->-1 // ->6
九、.inspect() →String
function inspect() { return ‘[‘ + this.map(Object.inspect).join(‘, ‘) + ‘]‘; }
返回數組的面向調試的字符串表示形式。
例子:
[‘Apples‘, {good: ‘yes‘, bad: ‘no‘}, 3, 34].inspect() // -> "[‘Apples‘, [object Object], 3, 34]"
十、.intersect(array) →Array
function intersect(array) { return this.uniq().findAll(function(item) { return array.indexOf(item) !== -1; }); }
返回包含在兩個給定數組之間相同的每個項目的數組。
Array方面Js底層代碼學習記錄