Prototype Array object Learning

Source: Internet
Author: User
Tags tojson

Copy codeThe Code is as follows:
Array. from = $;

(Function (){
// Reference the Array prototype
Var arrayProto = Array. prototype,
Slice = arrayProto. slice,
// JS 1.6 contains 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 JS1.6 is not used, _ each is set to the each method of the object.
// Here, the _ each method overwrites the _ each method in Enuerable.
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 non-null data in the Array
Function compact (){
Return this. select (function (value ){
Return value! = Null;
});
}

// Press the 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 (); // recursive call is provided here.
Array. push (value );
Return array;
});
}

Function (){
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 elements in the Array that are not repeated. If the Array is ordered, pass in the true parameter, and the execution will be 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;
});
}

// Obtain 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 concat of the array elements of the call method.
If (Object. isArray (item )&&! ('Callil' 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:,
Reverse: reverse,
Uniq: uniq,
Intersect: intersect,
Clone: clone,
ToArray: clone,
Size: size,
Inspect: inspect,
ToJSON: toJSON
});

// This bug is not found on the Internet. Who knows what to say?
Var CONCAT_ARGUMENTS_BUGGY = (function (){
Return []. concat (arguments) [0] [0]! = 1;
}) (1, 2)

If (CONCAT_ARGUMENTS_BUGGY) arrayProto. concat = concat;

// Check whether JS supports the indexOf and lastIndexOf methods. If not, set it to the method in 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
The following are examples of some methods. Simple methods are not provided.
Flatten ():
Copy codeThe Code is as follows:
['Frank', ['bob', 'lisa '], ['jill', ['Tom', 'Sally ']. flatten ()
//-> ['Frank', 'bob', 'lisa ', 'jill', 'Tom', 'Sally ']


Reduce (): This method means that if there is a data in the number, this data is directly returned; otherwise, the original array is returned.
Uniq ():
Copy codeThe 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 codeThe 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]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.