typescript中的Array类没有像AS3那样的sortOn方法,在github上找了一个库,https://github.com/gonchuki/m...就一个文件,但这个文件中sortOn方法是动态生成的。代码如下/*---script:Array.sortOn.jsdescription:AddsArray.sortOnfunctionandrelatedconstantsthatworkslikeinActionScriptforsortingarraysofobjects(applyingallsamestrictrules)license:MIT-stylelicense.authors:-gonchukidocs:http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/Array.html#sortOn()requires:-core/1.2.4:[Array]provides:-[Array.sortOn,Array.CASEINSENSITIVE,Array.DESCENDING,Array.UNIQUESORT,Array.RETURNINDEXEDARRAY,Array.NUMERIC]...*/Array.CASEINSENSITIVE=1;Array.DESCENDING=2;Array.UNIQUESORT=4;Array.RETURNINDEXEDARRAY=8;Array.NUMERIC=16;(function(){vardup_fn=function(field,field_options){varfiltered=(field_options&Array.NUMERIC)?this.map(function(item){returnitem[field].toFloat();}):(field_options&Array.CASEINSENSITIVE)?this.map(function(item){returnitem[field].toLowerCase();}):this.map(function(item){returnitem[field];});returnfiltered.length!==[].combine(filtered).length;};varsort_fn=function(item_a,item_b,fields,options){return(functionsort_by(fields,options){varret,a,b,opts=options[0],sub_fields=fields[0].match(/[^.]+/g);(functionget_values(s_fields,s_a,s_b){varfield=s_fields[0];if(s_fields.length>1){get_values(s_fields.slice(1),s_a[field],s_b[field]);}else{a=s_a[field].toString();b=s_b[field].toString();}})(sub_fields,item_a,item_b);if(opts&Array.NUMERIC){ret=(a.toFloat()-b.toFloat());}else{if(opts&Array.CASEINSENSITIVE){a=a.toLowerCase();b=b.toLowerCase();}ret=(a>b)?1:(a}if((ret===0)&&(fields.length>1)){ret=sort_by(fields.slice(1),options.slice(1));}elseif(opts&Array.DESCENDING){ret*=-1;}returnret;})(fields,options);};Array.implement({sortOn:function(fields,options){fields=$splat(fields);options=$splat(options);if(options.length!==fields.length)options=[];if((options[0]&Array.UNIQUESORT)&&(fields.some(function(field,i){returndup_fn(field,options[i]);})))return0;varcurry_sort=function(item_a,item_b){returnsort_fn(item_a,item_b,fields,options);};if(options[0]&Array.RETURNINDEXEDARRAY)returnthis.slice().sort(curry_sort);elsethis.sort(curry_sort);}});})();我在TS项目里增加了这个类的引用,并尝试写了.d.ts文件interfaceArray{ CASEINSENSITIVE;DESCENDING;UNIQUESORT;RETURNINDEXEDARRAY;NUMERIC;sortOn(fields,options):any;}代码里可以调用到sortOn方法,编译不报错。但运行时sortOn方法是undefined的。谁试过这种情况?该如何设置js文件,以及如何写这个.d.ts文件或者我怎么才能给Array加上sortOn方法?
明月笑刀无情
POPMUISE
相关分类