TypeScript扩展类方法
以数组删除元素为例
javascript数组删除一般是这样
const idx = selectedIDs.findIndex(x => x === deSelected);
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;
format(...replacements: string[]): string;
} }
实现
export { }; // this file needs to be a module
(function () {
if (!String.prototype.isNullOrEmpty) {
String.prototype.isNullOrEmpty = function (this: string): boolean {
return !this;
};
}
if (!String.prototype.format) {
String.prototype.format = function() {
var args = arguments;
return this.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined' ? args[number]: match ;
});
};}
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/
TypeScript扩展类方法的更多相关文章
- iOS 扩展类方法之category!
一.category介绍 category可以不修改源代码的基础上扩展新的方法,Category只能用于方法,不能用于成员变量. 二.category创建 Example:我们扩展NSString类新 ...
- javascript继承扩展类方法实现
javascript没有原生的继承语法,这确实很让人困惑,但是广大人民群从的智慧是无穷的.最近呢,正尝到一点从源码中学习的甜头,不分享一下实在难以平复激动的心情.前不久改造视频播放插件的时候,找到了v ...
- 玩转TypeScript(引言&文章目录) --初看TypeScript.
JavaScript过去一直被当作一种玩具语言存在,直到2005年以后,这门语言又开始活跃并可以说是火爆,而且随着浏览器版本的不断升级和完善,各种DOM之间的兼容性已经渐渐的被各种技术解决了,比如经典 ...
- 转职成为TypeScript程序员的参考手册
写在前面 作者并没有任何可以作为背书的履历来证明自己写作这份手册的分量. 其内容大都来自于TypeScript官方资料或者搜索引擎获得,期间掺杂少量作者的私见,并会标明. 大部分内容来自于http:/ ...
- 转载:TypeScript 简介与《TypeScript 中文入门教程》
简介 TypeScript是一种由微软开发的自由和开源的编程语言.它是JavaScript的一个超集,而且本质上向这个语言添加了可选的静态类型和基于类的面向对象编程.安德斯·海尔斯伯格,C#的首席架构 ...
- 转载:《TypeScript 中文入门教程》 14、输入.d.ts文件
版权 文章转载自:https://github.com/zhongsp 建议您直接跳转到上面的网址查看最新版本. 介绍 当使用外部JavaScript库或新的宿主API时,你需要一个声明文件(.d.t ...
- 使用Visual Studio Code搭建TypeScript开发环境
使用Visual Studio Code搭建TypeScript开发环境 1.TypeScript是干什么的 ? TypeScript是由微软Anders Hejlsberg(安德斯·海尔斯伯格,也是 ...
- 一个简单的 ASP.NET MVC 例子演示如何在 Knockout JS 的配合下,使用 TypeScript 。
前言 TypeScript 是一种由微软开发的自由和开源的编程语言.它是JavaScript的一个超集,而且本质上向这个语言添加了可选的静态类型和基于类的面向对象编程.安德斯·海尔斯伯格,C#的首席架 ...
- TypeScript札记:初体验
1.简介 TypeScript 是一种由微软开发的自由和开源的编程语言.它是JavaScript的一个超集,而且本质上向这个语言添加了可选的静态类型和基于类的面向对象编程. TypeScript是一种 ...
随机推荐
- 网页分页page
public class PageBean { private int page; // 第几页 private int pageSize; // 每页记录数 private int start; / ...
- SQL 基本查询语句
--使用数据库 use date go --创建表班级表 create table classInfo ( classNo ,),--主键约束使用primary key identity classN ...
- 在Ubuntu18.04下安装Java 11
一直以来,本人都使用第三方软件包"ppa:linuxuprising/java"安装Java JDK,最近一次安装时发现无法成功.这是由于现在无法直接从Oracle官网下载Java ...
- HDU6715 算术(莫比乌斯反演)
HDU6715 算术 莫比乌斯反演的变形. 对 \(\mu(lcm(i,j))\) 变换,易得 \(\mu(lcm(i,j)) = \mu(i)\cdot\mu(j)\cdot \mu(gcd(i,j ...
- Android SDK Android NDK Android Studio 官方下载地址<转>
转自:http://www.cnblogs.com/yaotong/archive/2011/01/25/1943615.html 2016.10Android Studio 2.2.1.0https ...
- zabbix自定义模板监控oracle
zabbix服务器端安装:zabbix-3.2.6.tar.gzzabbix client端安装:zabbix-agent-3.2.6-1.x86_64.rpm 1.首先必须在目标机器安装zabbix ...
- ETCD分布式锁实现选主机制(Golang实现)
ETCD分布式锁实现选主机制(Golang) 为什么要写这篇文章 做架构的时候,涉及到系统的一个功能,有一个服务必须在指定的节点执行,并且需要有个节点来做任务分发,想了半天,那就搞个主节点做这事呗,所 ...
- HttpServletRequest中的request.setAttribute()和request.getSession().setAttribute()
request.setAttribute("num",value):有效范围是一个请求范围,不发送请求的界面无法获取到value的值,jsp界面获取使用EL表达式${num}:re ...
- 【SD系列】SAP 查看销售订单时,报了一个错误消息,“项目不符合计划行(程序错误)”
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[SD系列]SAP 查看销售订单时,报了一个错误 ...
- IDEA-关闭自动保存&标志修改文件为星号(一)
IDEA优化 intellij 关闭自动保存