TypeScript扩展类方法

以数组删除元素为例

javascript数组删除一般是这样

1 const idx = selectedIDs.findIndex(x => x === deSelected);
2 selectedIDs.splice(idx, 1);    

或者 

const deleteId='xxxx';
const selectedIDs
= selectedIDs.filter(x => x!==deleteId)

不方便

在tyscript中扩展数组增加常用方法

1 建立接口声明文件

extension.d.ts

declare global {  
    interface Number {  
        thousandsSeperator(): String;  
    }  
}  
export {}; 

2 建立实现文件 number-extensions.ts

Number.prototype.thousandsSeperator = function(): string {  
    return Number(this).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');  
}  
export {}; 

3 使用

import "../utils/number-extensions";
const num=111111;
console.log(num.thousandsSeperator() );

4 数组常用方法

接口声明

export { }; // this file needs to be a module
declare global {
    interface Array<T> {
        firstOrDefault(predicate: (item: T) => boolean): T;
        where(predicate: (item: T) => boolean): T[];
        orderBy(propertyExpression: (item: T) => any): T[];
        orderByDescending(propertyExpression: (item: T) => any): T[];
        orderByMany(propertyExpressions: [(item: T) => any]): T[];
        orderByManyDescending(propertyExpressions: [(item: T) => any]): T[];
        remove(item: T): boolean;
        add(item: T): void;
        addRange(items: T[]): void;
        removeRange(items: T[]): void;
    }
    interface String {
        isNullOrEmpty(this: string): boolean;
  }
}

实现

export { }; // this file needs to be a module


(function () {
    if (!String.prototype.isNullOrEmpty) {
        String.prototype.isNullOrEmpty = function (this: string): boolean {
            return !this;
        };
    }

    if (!Array.prototype.firstOrDefault) {
        Array.prototype.firstOrDefault = function (predicate: (item: any) => boolean) {
            for (var i = 0; i < (<Array<any>>this).length; i++) {
                let item = (<Array<any>>this)[i];
                if (predicate(item)) {
                    return item;
                }
            }
            return null;
        }
    }

    if (!Array.prototype.where) {
        Array.prototype.where = function (predicate: (item: any) => boolean) {
            let result = [];
            for (var i = 0; i < (<Array<any>>this).length; i++) {
                let item = (<Array<any>>this)[i];
                if (predicate(item)) {
                    result.push(item);
                }
            }
            return result;
        }
    }

    if (!Array.prototype.remove) {
        Array.prototype.remove = function (item: any): boolean {
            let index = (<Array<any>>this).indexOf(item);
            if (index >= 0) {
                (<Array<any>>this).splice(index, 1);
                return true;
            }
            return false;
        }
    }

    if (!Array.prototype.removeRange) {
        Array.prototype.removeRange = function (items: any[]): void {
            for (var i = 0; i < items.length; i++) {
                (<Array<any>>this).remove(items[i]);
            }
        }
    }

    if (!Array.prototype.add) {
        Array.prototype.add = function (item: any): void {
            (<Array<any>>this).push(item);
        }
    }

    if (!Array.prototype.addRange) {
        Array.prototype.addRange = function (items: any[]): void {
            for (var i = 0; i < items.length; i++) {
                (<Array<any>>this).push(items[i]);
            }
        }
    }

    if (!Array.prototype.orderBy) {
        Array.prototype.orderBy = function (propertyExpression: (item: any) => any) {
            let result = [];
            var compareFunction = (item1: any, item2: any): number => {
                if (propertyExpression(item1) > propertyExpression(item2)) return 1;
                if (propertyExpression(item2) > propertyExpression(item1)) return -1;
                return 0;
            }
            for (var i = 0; i < (<Array<any>>this).length; i++) {
                return (<Array<any>>this).sort(compareFunction);

            }
            return result;
        }
    }

    if (!Array.prototype.orderByDescending) {
        Array.prototype.orderByDescending = function (propertyExpression: (item: any) => any) {
            let result = [];
            var compareFunction = (item1: any, item2: any): number => {
                if (propertyExpression(item1) > propertyExpression(item2)) return -1;
                if (propertyExpression(item2) > propertyExpression(item1)) return 1;
                return 0;
            }
            for (var i = 0; i < (<Array<any>>this).length; i++) {
                return (<Array<any>>this).sort(compareFunction);
            }
            return result;
        }
    }

    if (!Array.prototype.orderByMany) {
        Array.prototype.orderByMany = function (propertyExpressions: [(item: any) => any]) {
            let result = [];
            var compareFunction = (item1: any, item2: any): number => {
                for (var i = 0; i < propertyExpressions.length; i++) {
                    let propertyExpression = propertyExpressions[i];
                    if (propertyExpression(item1) > propertyExpression(item2)) return 1;
                    if (propertyExpression(item2) > propertyExpression(item1)) return -1;
                }
                return 0;
            }
            for (var i = 0; i < (<Array<any>>this).length; i++) {
                return (<Array<any>>this).sort(compareFunction);
            }
            return result;
        }
    }

    if (!Array.prototype.orderByManyDescending) {
        Array.prototype.orderByManyDescending = function (propertyExpressions: [(item: any) => any]) {
            let result = [];
            var compareFunction = (item1: any, item2: any): number => {
                for (var i = 0; i < propertyExpressions.length; i++) {
                    let propertyExpression = propertyExpressions[i];
                    if (propertyExpression(item1) > propertyExpression(item2)) return -1;
                    if (propertyExpression(item2) > propertyExpression(item1)) return 1;
                }
                return 0;
            }
            for (var i = 0; i < (<Array<any>>this).length; i++) {
                return (<Array<any>>this).sort(compareFunction);
            }
            return result;
        }
    }

})();

 

参考:

https://www.c-sharpcorner.com/article/learn-about-extension-methods-in-typescript/

 

转载于:https://www.cnblogs.com/wolbo/p/11271774.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值