SoFunction
Updated on 2025-04-13

Prototype Array Object Learning


= $A;

(function() {
//Reference to Array prototype
var arrayProto = ,
slice = ,
//There will be a native forEach method in JS 1.6
_each = ; // use native browser JS 1.6 implementation if available

function each(iterator) {
for (var i = 0, length = ; i < length; i++)
iterator(this[i]);
}
//If it is not JS1.6, set _each to the object's each method
//The _each method here covers the _each method in Enuerable
if (!_each) _each = each;

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

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

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

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

//Compress multi-dimensional array into one-dimensional array
function flatten() {
return ([], function(array, value) {
if ((value))
return (()); //There are recursive calls here
(value);
return array;
});
}

function without() {
var values = (arguments, 0);
return (function(value) {
return !(value);
});
}

function reverse(inline) {
return (inline !== false ? this : ())._reverse();
}

//Return all elements that do not repeat in the Array. If the array is ordered, pass in true parameters and execute it faster
function uniq(sorted) {
return ([], function(array, value, index) {
if (0 == index || (sorted ? () != value : !(value)))
(value);
return array;
});
}

//Get the intersection of two arrays
function intersect(array) {
return ().findAll(function(item) {
return (function(value) { return item === value });
});
}


function clone() {
return (this, 0);
}

function size() {
return ;
}

function inspect() {
return '[' + ().join(', ') + ']';
}

function toJSON() {
var results = [];
(function(object) {
var value = (object);
if (!(value)) (value);
});
return '[' + (', ') + ']';
}

function indexOf(item, i) {
i || (i = 0);
var 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) ? : (i < 0 ? + i : i) + 1;
var n = (0, i).reverse().indexOf(item);
return (n < 0) ? n : i - n - 1;
}

function concat() {
var array = (this, 0), item;
for (var i = 0, length = ; i < length; i++) {
item = arguments[i];
//The second condition is to prevent the array elements of the calling method from being concave together.
if ((item) && !('callee' in item)) {
for (var j = 0, arrayLength = ; j < arrayLength; j++)
(item[j]);
} else {
(item);
}
}
return array;
}

//The method in the mixin Enumerable
(arrayProto, Enumerable);

if (!arrayProto._reverse)
arrayProto._reverse = ;

(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 on the Internet, who knows to talk about it?
var CONCAT_ARGUMENTS_BUGGY = (function() {
return [].concat(arguments)[0][0] !== 1;
})(1,2)

if (CONCAT_ARGUMENTS_BUGGY) = concat;

// Check whether JS supports indexOf and lastIndexOf methods natively. If it does not support it, it will be set as a method inside the object.
if (!) = indexOf;
if (!) = lastIndexOf;
})();