Underscore.js 封装了常用的Js对象操作方法

创建一个.js文件,把下面的内置方法复制到该文件中:

// Underscore.js 1.9.1
// http://underscorejs.org
// © 2009-2018 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
// Underscore may be freely distributed under the MIT license.
!function(){var n=“object"typeof self&&self.self=self&&self||“object"typeof global&&global.global=global&&global||this||{},r=n.,e=Array.prototype,o=Object.prototype,s=“undefined”!=typeof Symbol?Symbol.prototype:null,u=e.push,c=e.slice,p=o.toString,i=o.hasOwnProperty,t=Array.isArray,a=Object.keys,l=Object.create,f=function(){},h=function(n){return n instanceof h?n:this instanceof h?void(this.wrapped=n):new h(n)};“undefined”==typeof exports||exports.nodeType?n.=h:(“undefined”!=typeof module&&!module.nodeType&&module.exports&&(exports=module.exports=h),exports.=h),h.VERSION=“1.9.1”;var v,y=function(u,i,n){if(void 0=i)return u;switch(nulln?3:n){case 1:return function(n){return u.call(i,n)};case 3:return function(n,r,t){return u.call(i,n,r,t)};case 4:return function(n,r,t,e){return u.call(i,n,r,t,e)}}return function(){return u.apply(i,arguments)}},d=function(n,r,t){return h.iteratee!v?h.iteratee(n,r):nulln?h.identity:h.isFunction(n)?y(n,r,t):h.isObject(n)&&!h.isArray(n)?h.matcher(n):h.property(n)};h.iteratee=v=function(n,r){return d(n,r,1/0)};var g=function(u,i){return i=nulli?u.length-1:+i,function(){for(var n=Math.max(arguments.length-i,0),r=Array(n),t=0;tn?void 0:n[r]}},j=function(n,r){return null!=n&&i.call(n,r)},x=function(n,r){for(var t=r.length,e=0;en)return;n=n[r[e]]}return t?n:void 0},=Math.pow(2,53)-1,A=b(“length”),w=function(n){var r=A(n);return"number"==typeof r&&0<=r&&r<=};h.each=h.forEach=function(n,r,t){var e,u;if(r=y(r,t),w(n))for(e=0,u=n.length;ee&&-1!e)return n[e]},h.filter=h.select=function(n,e,r){var u=[];return e=d(e,r),h.each(n,function(n,r,t){e(n,r,t)&&u.push(n)}),u},h.reject=function(n,r,t){return h.filter(n,h.negate(d®),t)},h.every=h.all=function(n,r,t){r=d(r,t);for(var e=!w(n)&&h.keys(n),u=(e||n).length,i=0;in)return;r=n[t]}return nullr?r:r.apply(n,e)})}),h.pluck=function(n,r){return h.map(n,h.property®)},h.where=function(n,r){return h.filter(n,h.matcher®)},h.findWhere=function(n,r){return h.find(n,h.matcher®)},h.max=function(n,e,r){var t,u,i=-1/0,o=-1/0;if(nulle||“number"typeof e&&“object”!=typeof n[0]&&null!=n)for(var a=0,c=(n=w(n)?n:h.values(n)).length;a=-1/0&&i===-1/0)&&(i=n,o=u)});return i},h.min=function(n,e,r){var t,u,i=1/0,o=1/0;if(nulle||"number"typeof e&&“object”!=typeof n[0]&&null!=n)for(var a=0,c=(n=w(n)?n:h.values(n)).length;a=1/0&&i=1/0)&&(i=n,o=u)});return i},h.shuffle=function(n){return h.sample(n,1/0)},h.sample=function(n,r,t){if(nullr||t)return w(n)||(n=h.values(n)),n[h.random(n.length-1)];var e=w(n)?h.clone(n):h.values(n),u=A(e);r=Math.max(Math.min(r,u),0);for(var i=u-1,o=0;oe){if(e=t)return 1;if(t=e)return-1}return n.index-r.index}),“value”)};var k=function(o,r){return function(e,u,n){var i=r?[[],[]]:{};return u=d(u,n),h.each(e,function(n,r){var t=u(n,r,e);o(i,n,t)}),i}};h.groupBy=k(function(n,r,t){j(n,t)?n[t].push®:n[t]=[r]}),h.indexBy=k(function(n,r,t){n[t]=r}),h.countBy=k(function(n,r,t){j(n,t)?n[t]++:n[t]=1});var S=/[^\ud800-\udfff]|[\ud800-\udbff][\udc00-\udfff]|[\ud800-\udfff]/g;h.toArray=function(n){return n?h.isArray(n)?c.call(n):h.isString(n)?n.match(S):w(n)?h.map(n,h.identity):h.values(n):[]},h.size=function(n){return nulln?0:w(n)?n.length:h.keys(n).length},h.partition=k(function(n,r,t){n[t?0:1].push®},!0),h.first=h.head=h.take=function(n,r,t){return nulln||n.length<1?nullr?void 0:[]:nullr||t?n[0]:h.initial(n,n.length-r)},h.initial=function(n,r,t){return c.call(n,0,Math.max(0,n.length-(nullr||t?1:r)))},h.last=function(n,r,t){return nulln||n.length<1?nullr?void 0:[]:nullr||t?n[n.length-1]:h.rest(n,Math.max(0,n.length-r))},h.rest=h.tail=h.drop=function(n,r,t){return c.call(n,nullr||t?1:r)},h.compact=function(n){return h.filter(n,Boolean)};var M=function(n,r,t,e){for(var u=(e=e||[]).length,i=0,o=A(n);i=l||u.push©,i=l):t?h.contains(i,l)||(i.push(l),u.push©):h.contains(u,c)||u.push©}return u},h.union=g(function(n){return h.uniq(M(n,!0,!0))}),h.intersection=function(n){for(var r=[],t=arguments.length,e=0,u=A(n);et&&r.push(i)}}return r},h.difference=g(function(n,r){return r=M(r,!0,!0),h.filter(n,function(n){return!h.contains(r,n)})}),h.unzip=function(n){for(var r=n&&h.max(n,A).length||0,t=Array®,e=0;etypeof t)0r?t:-1;if(r!=r)return 0<=(t=o(c.call(n,e,u),h.isNaN))?t+e:-1;for(t=0r)return t;return-1}};h.indexOf=E(1,h.findIndex,h.sortedIndex),h.lastIndexOf=E(-1,h.findLastIndex),h.range=function(n,r,t){nullr&&(r=n||0,n=0),t||(t=rr||r<1)return[];for(var t=[],e=0,u=n.length;eo?arguments[n++]:i[e];for(;n=u.leading?0:h.now(),i=null,c=t.apply(o,a),i||(o=a=null)},n=function(){var n=h.now();l||!1!u.leading||(l=n);var r=e-(n-l);return o=this,a=arguments,r<=0||e=u.trailing||(i=setTimeout(f,r)),c};return n.cancel=function(){clearTimeout(i),l=0,i=o=a=null},n},h.debounce=function(t,e,u){var i,o,a=function(n,r){i=null,r&&(o=t.apply(n,r))},n=g(function(n){if(i&&clearTimeout(i),u){var r=!i;i=setTimeout(a,e),r&&(o=t.apply(this,n))}else i=h.delay(a,e,this,n);return o});return n.cancel=function(){clearTimeout(i),i=null},n},h.wrap=function(n,r){return h.partial(r,n)},h.negate=function(n){return function(){return!n.apply(this,arguments)}},h.compose=function(){var t=arguments,e=t.length-1;return function(){for(var n=e,r=t[e].apply(this,arguments);n–;)r=t[n].call(this,r);return r}},h.after=function(n,r){return function(){if(–n<1)return r.apply(this,arguments)}},h.before=function(n,r){var t;return function(){return 0<–n&&(t=r.apply(this,arguments)),n<=1&&(r=null),t}},h.once=h.partial(h.before,2),h.restArguments=g;var I=!{toString:null}.propertyIsEnumerable(“toString”),T=[“valueOf”,“isPrototypeOf”,“toString”,“propertyIsEnumerable”,“hasOwnProperty”,“toLocaleString”],B=function(n,r){var t=T.length,e=n.constructor,u=h.isFunction(e)&&e.prototype||o,i=“constructor”;for(j(n,i)&&!h.contains(r,i)&&r.push(i);t–?(i=T[t])in n&&n[i]!u[i]&&!h.contains(r,i)&&r.push(i)};h.keys=function(n){if(!h.isObject(n))return[];if(a)return a(n);var r=[];for(var t in n)j(n,t)&&r.push(t);return I&&B(n,r),r},h.allKeys=function(n){if(!h.isObject(n))return[];var r=[];for(var t in n)r.push(t);return I&&B(n,r),r},h.values=function(n){for(var r=h.keys(n),t=r.length,e=Array(t),u=0;un)return n;for(var t=1;tn[a]||(n[a]=e[a])}return n}};h.extend=R(h.allKeys),h.extendOwn=h.assign=R(h.keys),h.findKey=function(n,r,t){r=d(r,t);for(var e,u=h.keys(n),i=0,o=u.length;in)return t;h.isFunction(e)?(1n)return!e;for(var u=Object(n),i=0;iu[o]||!(o in u))return!1}return!0},q=function(n,r,t,e){if(n=r)return 0!n||1/n1/r;if(nulln||nullr)return!1;if(n!=n)return r!=r;var u=typeof n;return(“function”=u||“object”=u||"object"typeof r)&&K(n,r,t,e)},K=function(n,r,t,e){n instanceof h&&(n=n._wrapped),r instanceof h&&(r=r._wrapped);var u=p.call(n);if(u!p.call®)return!1;switch(u){case"[object RegExp]":case"[object String]":return""+n""+r;case"[object Number]":return+n!=+n?+r!=+r:0+n?1/+n1/r:+n+r;case"[object Date]":case"[object Boolean]":return+n+r;case”[object Symbol]”:return s.valueOf.call(n)=s.valueOf.call®}var i="[object Array]"=u;if(!i){if(“object”!=typeof n||“object”!=typeof r)return!1;var o=n.constructor,a=r.constructor;if(o!a&&!(h.isFunction(o)&&o instanceof o&&h.isFunction(a)&&a instanceof a)&&"constructor"in n&&"constructor"in r)return!1}e=e||[];for(var c=(t=t||[]).length;c–;)if(t[c]=n)return e[c]=r;if(t.push(n),e.push®,i){if((c=n.length)!r.length)return!1;for(;c–;)if(!q(n[c],r[c],t,e))return!1}else{var l,f=h.keys(n);if(c=f.length,h.keys®.length!c)return!1;for(;c–;)if(l=f[c],!j(r,l)||!q(n[l],r[l],t,e))return!1}return t.pop(),e.pop(),!0},h.isEqual=function(n,r){return q(n,r)},h.isEmpty=function(n){return nulln||(w(n)&&(h.isArray(n)||h.isString(n)||h.isArguments(n))?0=n.length:0=h.keys(n).length)},h.isElement=function(n){return!(!n||1!n.nodeType)},h.isArray=t||function(n){return"[object Array]"=p.call(n)},h.isObject=function(n){var r=typeof n;return"function"=r||“object”=r&&!!n},h.each([“Arguments”,“Function”,“String”,“Number”,“Date”,“RegExp”,“Error”,“Symbol”,“Map”,“WeakMap”,“Set”,“WeakSet”],function®{h[“is”+r]=function(n){return p.call(n)="[object “+r+”]"}}),h.isArguments(arguments)||(h.isArguments=function(n){return j(n,“callee”)});var D=n.document&&n.document.childNodes;“function”!=typeof/./&&“object”!=typeof Int8Array&&“function”!=typeof D&&(h.isFunction=function(n){return"function"typeof n||!1}),h.isFinite=function(n){return!h.isSymbol(n)&&isFinite(n)&&!isNaN(parseFloat(n))},h.isNaN=function(n){return h.isNumber(n)&&isNaN(n)},h.isBoolean=function(n){return!0=n||!1=n||"[object Boolean]"=p.call(n)},h.isNull=function(n){return null=n},h.isUndefined=function(n){return void 0=n},h.has=function(n,r){if(!h.isArray®)return j(n,r);for(var t=r.length,e=0;en||!i.call(n,u))return!1;n=n[u]}return!!t},h.noConflict=function(){return n._=r,this},h.identity=function(n){return n},h.constant=function(n){return function(){return n}},h.noop=function(){},h.property=function®{return h.isArray®?function(n){return x(n,r)}:b®},h.propertyOf=function®{return nullr?function(){}:function(n){return h.isArray(n)?x(r,n):r[n]}},h.matcher=h.matches=function®{return r=h.extendOwn({},r),function(n){return h.isMatch(n,r)}},h.times=function(n,r,t){var e=Array(Math.max(0,n));r=y(r,t,1);for(var u=0;ur&&(r=n,n=0),n+Math.floor(Math.random()*(r-n+1))},h.now=Date.now||function(){return(new Date).getTime()};var L={"&":"&","<":"<",">":">",’"’:""","’":"’","`":"`"},P=h.invert(L),W=function®{var t=function(n){return r[n]},n="(?:"+h.keys®.join("|")+")",e=RegExp(n),u=RegExp(n,“g”);return function(n){return n=nulln?"":""+n,e.test(n)?n.replace(u,t):n}};h.escape=W(L),h.unescape=W§,h.result=function(n,r,t){h.isArray®||(r=[r]);var e=r.length;if(!e)return h.isFunction(t)?t.call(n):t;for(var u=0;un?void 0:n[r[u]];void 0===i&&(i=t,u=e),n=h.isFunction(i)?i.call(n):i}return n};var C=0;h.uniqueId=function(n){var r=++C+”";return n?n+r:r},h.templateSettings={evaluate:/<%([\s\S]+?)%>/g,interpolate:/<%=([\s\S]+?)%>/g,escape:/<%-([\s\S]+?)%>/g};var J=/(.)^/,U={"’":"’","\":"\","\r":“r”,"\n":“n”,"\u2028":“u2028”,"\u2029":“u2029”},V=/\|’|\r|\n|\u2028|\u2029/g,KaTeX parse error: Expected '}', got '&' at position 60: …ction(i,n,r){!n&̲&r&&(n=r),n=h.d…",“g”),o=0,a="__p+=’";i.replace(e,function(n,r,t,e,u){return a+=i.slice(o,u).replace(V,$),o=u+n.length,r?a+="’+\n((__t=("+r+"))==null?’’:.escape(__t))+\n’":t?a+="’+\n((__t=("+t+"))==null?’’:__t)+\n’":e&&(a+="’;\n"+e+"\n__p+=’"),n}),a+="’;\n",n.variable||(a=“with(obj||{}){\n”+a+"}\n"),a=“var __t,__p=’’,__j=Array.prototype.join,”+“print=function(){__p+=__j.call(arguments,’’);};\n”+a+“return __p;\n”;try{t=new Function(n.variable||“obj”,"",a)}catch(n){throw n.source=a,n}var u=function(n){return t.call(this,n,h)},c=n.variable||“obj”;return u.source=“function(”+c+"){\n"+a+"}",u},h.chain=function(n){var r=h(n);return r._chain=!0,r};var G=function(n,r){return n._chain?h®.chain():r};h.mixin=function(t){return h.each(h.functions(t),function(n){var r=h[n]=t[n];h.prototype[n]=function(){var n=[this._wrapped];return u.apply(n,arguments),G(this,r.apply(h,n))}}),h},h.mixin(h),h.each([“pop”,“push”,“reverse”,“shift”,“sort”,“splice”,“unshift”],function®{var t=e[r];h.prototype[r]=function(){var n=this._wrapped;return t.apply(n,arguments),“shift”!==r&&“splice”!==r||0!==n.length||delete n[0],G(this,n)}}),h.each([“concat”,“join”,“slice”],function(n){var r=e[n];h.prototype[n]=function(){return G(this,r.apply(this._wrapped,arguments))}}),h.prototype.value=function(){return this._wrapped},h.prototype.valueOf=h.prototype.toJSON=h.prototype.value,h.prototype.toString=function(){return String(this._wrapped)},“function”==typeof define&&define.amd&&define(“underscore”,[],function(){return h})}();

你可能感兴趣的:(JavaScript,Underscore封装)