TypeScript 学习笔记:属性索引器
来源:互联网 发布:openwrt 网络唤醒 编辑:程序博客网 时间:2024/06/06 16:05
export interface StringValidator { isAcceptable(s: string): boolean;}const lettersRegexp = /^[A-Za-z]+$/;export class LettersOnlyValidator implements StringValidator { isAcceptable(s: string) { return lettersRegexp.test(s); }}const numberRegexp = /^[0-9]+$/;export class ZipCodeValidator implements StringValidator { isAcceptable(s: string) { return s.length === 5 && numberRegexp.test(s); }}// 开始测试let strings = ["Hello", "98052", "101"];// Validators to uselet validators: { [key: string]: StringValidator; } = {};validators["邮政编码"] = new ZipCodeValidator();validators["纯字符"] = new LettersOnlyValidator();// Show whether each string passed each validatorstrings.forEach(stringItem => { for (let valName in validators) { console.log(`"${stringItem}" - ${validators[valName].isAcceptable(stringItem) ? "匹配" : "不匹配"} ${valName}`); }});
运行结果如下:
"Hello" - 不匹配 邮政编码"Hello" - 匹配 纯字符"98052" - 匹配 邮政编码"98052" - 不匹配 纯字符"101" - 不匹配 邮政编码"101" - 不匹配 纯字符
我是真搞不懂这一句,太奇妙....
- let validators: { [key: string]: StringValidator; } = {};
阅读全文
0 0
- TypeScript 学习笔记:属性索引器
- TypeScript 学习笔记
- TypeScript 学习笔记1
- TypeScript 学习笔记2
- TYPESCRIPT 学习笔记3
- TypeScript学习笔记一
- TypeScript 学习笔记
- Typescript学习笔记
- TypeScript学习笔记
- TypeScript学习笔记一
- TypeScript 的索引签名 的笔记
- TypeScript学习笔记之 类
- TypeScript学习笔记之函数
- TypeScript学习笔记之 泛型
- TypeScript 学习笔记1: 简介
- TypeScript 学习笔记6: Classes
- TypeScript 学习笔记7: Generics
- TypeScript 学习笔记8: Modules
- codeblocks运行时提示Can't find compiler executabl...
- 字符集与编码
- 文章标题
- 最新版Oracle数据库的PLSQL安装链接服务器
- Error:Execution failed for task transformClassesWithDexForDebug
- TypeScript 学习笔记:属性索引器
- ios10 禁止用户缩放失效
- How to output the message when using the function 'printf'
- Freemarker TemplateLoader.findTemplateSource("WEB-INF/template/index.html"): Not found错误原因
- form表单提交方式
- 面试可能问到的问题
- 大数据的应用:给顾客打上几百个标签
- Java SimpleDateFormat进行日期格式化
- 为何要向首屈一指的RagingWire数据中心公司看齐