Copy Code code as follows:
/*!
* Jlip JavaScript Library v0.1
*
* Copyright, Lip2up (lip2up@qq.com)
* Just for free use, NO LICENSE
*/
(function () {
function extend (target, props) {
For (var m in props) {
if (target[m] = = undefined) target[m] = props[m];
}
}
var Fns = {every:1, some:2, Foreach:3, Map:4, Filter:5},
Reduceerror = ' Reduce of empty array with no initial value ';
function each (FN, _this, kind) {
var len = this.length, ret = kind = = Fns.filter? []
: kind = = Fns.map? Array (len): Undefined,
Find = Kind = = Fns.some, I, V;
for (i = 0; i < len; i++) {
if (This[i]!== undefined) {
v = Fn.call (_this, this[i], I, this);
Switch (kind) {
Case Fns.every:
Case Fns.some:
if (v = = find) return to find;
Break
Case FNS.MAP:
Ret[i] = v;
Break
Case Fns.filter:
if (v = = true) Ret[ret.length] = this[i];
Break
}
}
}
Return Kind >= Fns.foreach? RET:!find;
}
function reduce (FN, init, right) {
var len = this.length, I, prev, inc = right? -1:1;
if (len = = 0 && init = = undefined)
Throw TypeError (Reduceerror);
for (i = right? len-1:0, prev = init;
Prev = = undefined && (right I >= 0:i < Len);
i = = Inc) {
prev = This[i];
}
if (prev = = undefined && i = = (right -1:len))
Throw TypeError (Reduceerror);
for (; (right? I >= 0:i < Len); i = = Inc) {
if (This[i]!== undefined)
Prev = fn (prev, this[i], I, this);
}
return prev;
}
Extend (Array.prototype, {
Every:function (FN, _this) {
Return Each.call (This, FN, _this, fns.every);
},
Some:function (FN, _this) {
Return Each.call (This, FN, _this, fns.some);
},
Foreach:function (FN, _this) {
Return Each.call (This, FN, _this, Fns.foreach);
},
Map:function (FN, _this) {
Return Each.call (This, FN, _this, Fns.map);
},
Filter:function (FN, _this) {
Return Each.call (This, FN, _this, fns.filter);
},
Reduce:function (FN, init) {
Return Reduce.call (This, FN, init);
},
Reduceright:function (FN, init) {
Return Reduce.call (This, FN, Init, true);
}
});
})();