(function () { var root \= this; var breaker \= {}; var ArrayProto \= Array.prototype, ObjProto \= Object.prototype; var nativeForEach \= ArrayProto.forEach,
nativeMap \= ArrayProto.map,
nativeFilter \= ArrayProto.filter,
nativeKeys \= Object.keys; var slice \= ArrayProto.slice,
unshift \= ArrayProto.unshift,
hasOwnProperty \= ObjProto.hasOwnProperty;
var \_ \= function (obj) { return new wrapper(obj); };
root.\_ \= \_;
\_.isNumber \= function (obj) { return !!(obj \=== 0 || (obj && obj.toExponential && obj.toFixed));
};
\_.isFunction \= function (obj) { return !!(obj && obj.constructor && obj.call && obj.apply);
};
var each \= \_.each \= \_.forEach \= function (obj, iterator, context) { if (obj \=== null) return; if (nativeForEach && obj.forEach \=== nativeForEach) {
obj.forEach(iterator, context);
} else if (\_.isNumber(obj.length)) { for (var i \= 0, l \= obj.length; i < l; i++) { if (iterator.call(context, obj\[i\], i, obj) \=== breaker) return;
}
} else { for (var key in obj) { if (hasOwnProperty.call(obj, key)) { if (iterator.call(context, obj\[key\], key, obj) \=== breaker) return;
}
}
}
};
\_.functions \= \_.methods \= function (obj) { return \_.filter(\_.keys(obj), function (key) { return \_.isFunction(obj\[key\])}).sort();
};
\_.filter \= \_.select \= function (obj, iterator, context) { var results \= \[\]; if (obj \== null) return results; if (nativeFilter && obj.filter \=== nativeFilter) return obj.filter(iterator, context);
each(obj, function (value, index, list) { if (iterator.call(context, value, index, list)) results\[results.length\] \= value;
}); return results;
};
\_.keys \= nativeKeys || function (obj) { if (obj !== Object(obj)) throw new TypeError('Invalid object'); var keys \= \[\]; for (var key in obj) if (hasOwnProperty.call(obj, key)) keys\[keys.length\] \= key; return keys;
};
\_.map \= function (obj, iterator, context) { var results \= \[\]; if (obj \== null) return results; if (nativeMap && obj.map \=== nativeMap) return obj.map(iterator, context);
each(obj, function (value, index, list) {
results\[results.length\] \= iterator.call(context, value, index, list);
}); return results;
};
var wrapper \= function (obj) { this.\_wrapped \= obj; };
\_.prototype \= wrapper.prototype;
\_.mixin \= function (obj) {
each(\_.functions(obj), function (name) {
addToWrapper(name, \_\[name\] \= obj\[name\]);
});
};
var result \= function (obj, chain) { return chain ? \_(obj).chain() : obj;
};
var addToWrapper \= function (name, func) {
wrapper.prototype\[name\] \= function () { var args \= slice.call(arguments);
unshift.call(args, this.\_wrapped); return result(func.apply(\_, args), this.\_chain);
};
};
\_.mixin(\_);
each(\['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'\], function (name) { var method \= ArrayProto\[name\];
wrapper.prototype\[name\] \= function () {
method.apply(this.\_wrapped, arguments); return result(this.\_wrapped, this.\_chain);
};
});
each(\['concat', 'join', 'slice'\], function (name) { var method \= ArrayProto\[name\];
wrapper.prototype\[name\] \= function () { return result(method.apply(this.\_wrapped, arguments), this.\_chain);
};
});
wrapper.prototype.chain \= function () { this.\_chain \= true; return this;
};
wrapper.prototype.value \= function () { return this.\_wrapped;
};
})();
var re \= \_(\[1,2,3\]).chain().map(function (val) { return val \* 2;
}).filter(function (val) { return val < 5;
}).pop().concat(\['5'\]).value();
alert(re);