币秋缠论培训 發表於 2019-7-30 18:03:00

TypeScript扩展类方法

<p>以数组删除元素为例</p>
<p>javascript数组删除一般是这样</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> const idx = selectedIDs.findIndex(x =&gt; x ===<span style="color: rgba(0, 0, 0, 1)"> deSelected);
</span><span style="color: rgba(0, 128, 128, 1)">2</span> selectedIDs.splice(idx, 1);    </pre>
</div>
<p>或者&nbsp;</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">const deleteId='xxxx'<span>;</span><br>const selectedIDs</span>= selectedIDs.filter(x =&gt; x!==deleteId)</pre>
</div>
<p>不方便</p>
<p>在tyscript中扩展数组增加常用方法</p>
<p>1 建立接口声明文件</p>
<p>extension.d.ts</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">declare global {
    interface Number {
      thousandsSeperator(): String;
    }
}</span><span style="color: rgba(0, 0, 0, 1)">
export {}; </span></pre>
</div>
<p>2 建立实现文件 number-extensions.ts</p>
<div class="cnblogs_code">
<pre>Number.prototype.thousandsSeperator = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(): string {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> Number(<span style="color: rgba(0, 0, 255, 1)">this</span>).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ','<span style="color: rgba(0, 0, 0, 1)">);
}<br></span></pre>
<pre><span>export {}; </span></pre>
</div>
<p>3 使用</p>
<div class="cnblogs_code">
<pre>import "../utils/number-extensions"<span style="color: rgba(0, 0, 0, 1)">;
const num</span>=111111<span style="color: rgba(0, 0, 0, 1)">;
console.log(num.thousandsSeperator() );</span></pre>
</div>
<p>4 数组常用方法</p>
<p>接口声明</p>
<div class="cnblogs_code">
<pre>export { }; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> this file needs to be a module</span>
<span style="color: rgba(0, 0, 0, 1)">declare global {
    interface Array</span>&lt;T&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      firstOrDefault(predicate: (item: T) </span>=&gt; <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)">): T;
      where(predicate: (item: T) </span>=&gt; <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)">): T[];
      orderBy(propertyExpression: (item: T) </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> any): T[];
      orderByDescending(propertyExpression: (item: T) </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> any): T[];
      orderByMany(propertyExpressions: [(item: T) </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> any]): T[];
      orderByManyDescending(propertyExpressions: [(item: T) </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> any]): T[];
      remove(item: T): </span><span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)">;
      add(item: T): </span><span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)">;
      addRange(items: T[]): </span><span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)">;
      removeRange(items: T[]): </span><span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)">;
    }
    interface String {
      isNullOrEmpty(</span><span style="color: rgba(0, 0, 255, 1)">this</span>: string): <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)">;<br></span><code>     format(...replacements: string[]): string;</code></pre>
<pre><span style="color: rgba(0, 0, 0, 1)">} }</span></pre>
</div>
<p>实现</p>
<div class="cnblogs_code">
<pre>export { }; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> this file needs to be a module</span>
<span style="color: rgba(0, 0, 0, 1)">

(</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> () {
    </span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 0, 1)">String.prototype.isNullOrEmpty) {
      String.prototype.isNullOrEmpty </span>= <span style="color: rgba(0, 0, 255, 1)">function</span> (<span style="color: rgba(0, 0, 255, 1)">this</span>: string): <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> !<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
      };
    }<br></span></pre>
<pre><span>    if (!<span>String.prototype.</span></span><code>format</code><em>) { <br></em></pre>
<pre><code>    String.prototype.format = function() {
        var args = arguments;
        return this.replace(/{(\d+)}/g, function(match, number) {
        return typeof args != 'undefined' ? args: match ;
        });
   };</code><em id="__mceDel"><em id="__mceDel">}</em></em></pre>
<pre><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 0, 1)">Array.prototype.firstOrDefault) { Array.prototype.firstOrDefault </span>= <span style="color: rgba(0, 0, 255, 1)">function</span> (predicate: (item: any) =&gt; <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)">) { </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">var</span> i = 0; i &lt; (&lt;Array&lt;any&gt;&gt;<span style="color: rgba(0, 0, 255, 1)">this</span>).length; i++<span style="color: rgba(0, 0, 0, 1)">) { let item </span>= (&lt;Array&lt;any&gt;&gt;<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (predicate(item)) { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> item; } } </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; } } </span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 0, 1)">Array.prototype.where) { Array.prototype.where </span>= <span style="color: rgba(0, 0, 255, 1)">function</span> (predicate: (item: any) =&gt; <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)">) { let result </span>=<span style="color: rgba(0, 0, 0, 1)"> []; </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">var</span> i = 0; i &lt; (&lt;Array&lt;any&gt;&gt;<span style="color: rgba(0, 0, 255, 1)">this</span>).length; i++<span style="color: rgba(0, 0, 0, 1)">) { let item </span>= (&lt;Array&lt;any&gt;&gt;<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (predicate(item)) { result.push(item); } } </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> result; } } </span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 0, 1)">Array.prototype.remove) { Array.prototype.remove </span>= <span style="color: rgba(0, 0, 255, 1)">function</span> (item: any): <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> { let index </span>= (&lt;Array&lt;any&gt;&gt;<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">).indexOf(item); </span><span style="color: rgba(0, 0, 255, 1)">if</span> (index &gt;= 0<span style="color: rgba(0, 0, 0, 1)">) { (</span>&lt;Array&lt;any&gt;&gt;<span style="color: rgba(0, 0, 255, 1)">this</span>).splice(index, 1<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">; } </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">; } } </span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 0, 1)">Array.prototype.removeRange) { Array.prototype.removeRange </span>= <span style="color: rgba(0, 0, 255, 1)">function</span> (items: any[]): <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">var</span> i = 0; i &lt; items.length; i++<span style="color: rgba(0, 0, 0, 1)">) { (</span>&lt;Array&lt;any&gt;&gt;<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">).remove(items); } } } </span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 0, 1)">Array.prototype.add) { Array.prototype.add </span>= <span style="color: rgba(0, 0, 255, 1)">function</span> (item: any): <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> { (</span>&lt;Array&lt;any&gt;&gt;<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">).push(item); } } </span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 0, 1)">Array.prototype.addRange) { Array.prototype.addRange </span>= <span style="color: rgba(0, 0, 255, 1)">function</span> (items: any[]): <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">var</span> i = 0; i &lt; items.length; i++<span style="color: rgba(0, 0, 0, 1)">) { (</span>&lt;Array&lt;any&gt;&gt;<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">).push(items); } } } </span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 0, 1)">Array.prototype.orderBy) { Array.prototype.orderBy </span>= <span style="color: rgba(0, 0, 255, 1)">function</span> (propertyExpression: (item: any) =&gt;<span style="color: rgba(0, 0, 0, 1)"> any) { let result </span>=<span style="color: rgba(0, 0, 0, 1)"> []; </span><span style="color: rgba(0, 0, 255, 1)">var</span> compareFunction = (item1: any, item2: any): number =&gt;<span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 0, 255, 1)">if</span> (propertyExpression(item1) &gt; propertyExpression(item2)) <span style="color: rgba(0, 0, 255, 1)">return</span> 1<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">if</span> (propertyExpression(item2) &gt; propertyExpression(item1)) <span style="color: rgba(0, 0, 255, 1)">return</span> -1<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">return</span> 0<span style="color: rgba(0, 0, 0, 1)">; } </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">var</span> i = 0; i &lt; (&lt;Array&lt;any&gt;&gt;<span style="color: rgba(0, 0, 255, 1)">this</span>).length; i++<span style="color: rgba(0, 0, 0, 1)">) { </span><span style="color: rgba(0, 0, 255, 1)">return</span> (&lt;Array&lt;any&gt;&gt;<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">).sort(compareFunction); } </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> result; } } </span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 0, 1)">Array.prototype.orderByDescending) { Array.prototype.orderByDescending </span>= <span style="color: rgba(0, 0, 255, 1)">function</span> (propertyExpression: (item: any) =&gt;<span style="color: rgba(0, 0, 0, 1)"> any) { let result </span>=<span style="color: rgba(0, 0, 0, 1)"> []; </span><span style="color: rgba(0, 0, 255, 1)">var</span> compareFunction = (item1: any, item2: any): number =&gt;<span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 0, 255, 1)">if</span> (propertyExpression(item1) &gt; propertyExpression(item2)) <span style="color: rgba(0, 0, 255, 1)">return</span> -1<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">if</span> (propertyExpression(item2) &gt; propertyExpression(item1)) <span style="color: rgba(0, 0, 255, 1)">return</span> 1<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">return</span> 0<span style="color: rgba(0, 0, 0, 1)">; } </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">var</span> i = 0; i &lt; (&lt;Array&lt;any&gt;&gt;<span style="color: rgba(0, 0, 255, 1)">this</span>).length; i++<span style="color: rgba(0, 0, 0, 1)">) { </span><span style="color: rgba(0, 0, 255, 1)">return</span> (&lt;Array&lt;any&gt;&gt;<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">).sort(compareFunction); } </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> result; } } </span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 0, 1)">Array.prototype.orderByMany) { Array.prototype.orderByMany </span>= <span style="color: rgba(0, 0, 255, 1)">function</span> (propertyExpressions: [(item: any) =&gt;<span style="color: rgba(0, 0, 0, 1)"> any]) { let result </span>=<span style="color: rgba(0, 0, 0, 1)"> []; </span><span style="color: rgba(0, 0, 255, 1)">var</span> compareFunction = (item1: any, item2: any): number =&gt;<span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">var</span> i = 0; i &lt; propertyExpressions.length; i++<span style="color: rgba(0, 0, 0, 1)">) { let propertyExpression </span>=<span style="color: rgba(0, 0, 0, 1)"> propertyExpressions; </span><span style="color: rgba(0, 0, 255, 1)">if</span> (propertyExpression(item1) &gt; propertyExpression(item2)) <span style="color: rgba(0, 0, 255, 1)">return</span> 1<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">if</span> (propertyExpression(item2) &gt; propertyExpression(item1)) <span style="color: rgba(0, 0, 255, 1)">return</span> -1<span style="color: rgba(0, 0, 0, 1)">; } </span><span style="color: rgba(0, 0, 255, 1)">return</span> 0<span style="color: rgba(0, 0, 0, 1)">; } </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">var</span> i = 0; i &lt; (&lt;Array&lt;any&gt;&gt;<span style="color: rgba(0, 0, 255, 1)">this</span>).length; i++<span style="color: rgba(0, 0, 0, 1)">) { </span><span style="color: rgba(0, 0, 255, 1)">return</span> (&lt;Array&lt;any&gt;&gt;<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">).sort(compareFunction); } </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> result; } } </span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 0, 1)">Array.prototype.orderByManyDescending) { Array.prototype.orderByManyDescending </span>= <span style="color: rgba(0, 0, 255, 1)">function</span> (propertyExpressions: [(item: any) =&gt;<span style="color: rgba(0, 0, 0, 1)"> any]) { let result </span>=<span style="color: rgba(0, 0, 0, 1)"> []; </span><span style="color: rgba(0, 0, 255, 1)">var</span> compareFunction = (item1: any, item2: any): number =&gt;<span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">var</span> i = 0; i &lt; propertyExpressions.length; i++<span style="color: rgba(0, 0, 0, 1)">) { let propertyExpression </span>=<span style="color: rgba(0, 0, 0, 1)"> propertyExpressions; </span><span style="color: rgba(0, 0, 255, 1)">if</span> (propertyExpression(item1) &gt; propertyExpression(item2)) <span style="color: rgba(0, 0, 255, 1)">return</span> -1<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">if</span> (propertyExpression(item2) &gt; propertyExpression(item1)) <span style="color: rgba(0, 0, 255, 1)">return</span> 1<span style="color: rgba(0, 0, 0, 1)">; } </span><span style="color: rgba(0, 0, 255, 1)">return</span> 0<span style="color: rgba(0, 0, 0, 1)">; } </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">var</span> i = 0; i &lt; (&lt;Array&lt;any&gt;&gt;<span style="color: rgba(0, 0, 255, 1)">this</span>).length; i++<span style="color: rgba(0, 0, 0, 1)">) { </span><span style="color: rgba(0, 0, 255, 1)">return</span> (&lt;Array&lt;any&gt;&gt;<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">).sort(compareFunction); } </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> result; } } })();</span></pre>
</div>
<p>&nbsp;</p>
<p>参考:</p>
<p>https://www.c-sharpcorner.com/article/learn-about-extension-methods-in-typescript/</p>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/wolbo/p/11271774.html
頁: [1]
查看完整版本: TypeScript扩展类方法