ArrayList class (enhanced version) _ basic knowledge

Source: Internet
Author: User
ArrayList class (enhanced edition) Author: moonlight
From: http://bbs.51js.com/thread-66469-1-1.html

The Code is as follows:


Script
Function ArrayList ()
{
Var ins = Array. apply (this, arguments );
Ins. constructor = arguments. callee;
Ins. base = Array;

Ins. each = function (closure)
{
If (typeof closure = 'undefined ')
Closure = function (x) {return x };
If (typeof closure! = 'Function ')
{
Var c = closure;
Closure = function (x) {return x = c}
}

Var ret = new ArrayList ();
Var args = Array. apply (this, arguments). slice (1 );

For (var I = 0; I <this. length; I ++)
{
Var rval = closure. apply (this, [this [I]. concat (args). concat (I ))
If (rval | rval = 0)
Ret. push (rval );
}

Return ret;
}

Ins. trim = function ()
{
Return this. each. apply (this );
}

Ins. all = function (closure)
{
Return this. each. apply (this, arguments). length = this. length;
}

Ins. any = function (closure)
{
Return this. each. apply (this, arguments). length> 0;
}

Ins. contains = function (el)
{
Return this. any (function (x) {return x = el });
}

Ins. indexOf = function (el)
{
Var ret = this. each. call (this, function (x, I) {return el = x? I: false}) [0];
Return ret? Ret:-1;
}

Ins. subarr = function (start, end)
{
End = end | Math. Infinity;
Return this. each. call (this, function (x, I) {return I> = start & I <end? X: null });
}

Ins. valueOf = ins. toString;

Ins. toString = function ()
{
Return '[' + this. valueOf () + ']';
}

Ins. map = function (list, closure)
{
If (typeof list = 'function' & typeof closure! = 'Function ')
{
Var li = closure;
Closure = list;
List = li;
}
Closure = closure | ArrayList;

Return this. each. call (this, function (x, I) {return closure. call (this, x, list [I])});
};

Ins. slice = function ()
{
Return this. constructor (ins. base. prototype. slice. apply (this, arguments ));
}

Ins. splice = function ()
{
Return this. constructor (ins. base. prototype. splice. apply (this, arguments ));
}

Ins. concat = function ()
{
Return this. constructor (ins. base. prototype. concat. apply (this, arguments ));
}

Return ins;
}

Var a = new ArrayList (1, 2, 3 );
Alert (a. length );
Alert ();
Alert (a instanceof Array );
Alert (a. constructor );
Alert (a instanceof ArrayList); // unfortunately, this value is incorrect, but it cannot be implemented, so you have to give up.

Alert (a. each (function (x) {return x + x }));
Alert (a. all (function (x) {return x> 0 }));
Alert (a. all (function (x) {return x <1 }));
Alert (a. any (function (x) {return x = 2 }));

Alert (a. contains (2 ));
Alert (a. contains (-1 ));

Var B = a. map ([3, 2], function (x, y) {return x + y });
Alert (B );
Alert (a. map ([2, 3, 4]);

Alert (a. indexOf (2 ));
Alert (a. indexOf (-1 ));

Alert (a. subarr (1, 3 ));
Alert (a. toString ());
Var B = new ArrayList (a, );
Alert (B. toString ());
Alert (B. slice (1 ));
Script

Arr. all returns true if all elements in an array (SET) meet the conditions. Otherwise, false is returned.
Arr. any returns true if any of the elements in an array (SET) meets the conditions. If none of the conditions are met, false is returned.
Arr. each returns a child array consisting of every element that meets the condition.
Arr. map matches two arrays (sets) and uses the specified closure for calculation.
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.