Home  >  Article  >  Web Front-end  >  Prototype Array object learning_prototype

Prototype Array object learning_prototype

WBOY
WBOYOriginal
2016-05-16 18:49:44860browse
Copy code The code is as follows:

Array.from = $A;

(function () {
//Reference of Array prototype
var arrayProto = Array.prototype,
slice = arrayProto.slice,
//JS 1.6 will have the native forEach method
_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]);
}
//If it is not JS1.6, _each is set to the each method of the object
//The _each method here overrides the _each in Enuerable Method
if (!_each) _each = each;

function clear() {
this.length = 0;
return this;
}

function first() {
return this[0];
}

function last() {
return this[this.length - 1];
}

//Return all data in Array that are not null
function compact() {
return this.select(function(value) {
return value != null;
});
}

//Press a multi-dimensional array into a one-dimensional array
function flatten() {
return this.inject([], function(array, value) {
if (Object. isArray(value))
return array.concat(value.flatten()); //There is a recursive call here
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();
}

//Return all non-repeating elements in the Array. If the array is ordered, passing in the true parameter will execute faster
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;
});
}

//Get the intersection of two arrays
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];
//The second condition is to prevent the array elements of the calling method from also being concat up
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 Methods in 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
});

//I haven’t found this bug online. Does anyone know how to tell it?
var CONCAT_ARGUMENTS_BUGGY = (function() {
return [].concat(arguments)[0][0] !== 1;
})(1,2)

if (CONCAT_ARGUMENTS_BUGGY) arrayProto.concat = concat;

//Check whether JS natively supports the indexOf and lastIndexOf methods. If not, set them to methods within the object
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
Some examples of methods are given below. Examples will not be given for simple methods.
flatten():
Copy code The code is as follows:

['frank', ['bob', 'lisa'], ['jill', ['tom', 'sally']]].flatten()
// -> ['frank', 'bob', 'lisa' , 'jill', 'tom', 'sally']


reduce(): The meaning of this method is that if there is a data in the number, this data will be returned directly, otherwise it will be returned Original array
uniq():
Copy code The code is as follows:

[' Sam', 'Justin', 'Andrew', 'Dan', 'Sam'].uniq();
// -> ['Sam', 'Justin', 'Andrew', 'Dan']

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


without():
Copy code The code is as follows:

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

[3, 5, 6, 1, 20].without( 20, 6)
// -> [3, 5, 1]
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn