博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[TypeScript] Create a fluent API using TypeScript classes
阅读量:7169 次
发布时间:2019-06-29

本文共 757 字,大约阅读时间需要 2 分钟。

You can create an easy to chain API using TypeScript classes. Learn about the thisreturn type annotation and how it plays with function chaining and class inheritance.

 

class Adder {  protected acc: number = 0;  add(num: number): Adder {    this.acc += num;    return this; // enable to chain methods  }  get result() {    return this.acc;  }}const adder = new Adder()const res = adder.add(1).add(2).result;console.log(res); // 3class Calculator extends Adder {  subtract(num: number): Calculator {    this.acc -= num;    return this;  }}const cal = new Calculator();const res2 = cal.add(1).add(2).subtract(100).result;console.log(res2) // -97

 

You can also do:

const res2 = new Calculator()  .add(1)  .add(2)  .subtract(100)  .result;console.log(res2) // -97

 

转载地址:http://lxmwm.baihongyu.com/

你可能感兴趣的文章
***四种参数传递的形式——URL,超链接,js,form表单
查看>>
LINUX下PHP编译添加相应的动态扩展模块so(不需要重新编译PHP,以openssl.so为例)...
查看>>
11.21团队总结
查看>>
Application Metrics With Spring Boot Actuator
查看>>
ML科普系列(三)监督学习和无监督学习
查看>>
Python——functools
查看>>
pintos Project (1) 简介
查看>>
桌面图标编程其他的图标解决
查看>>
Java多线程系列 JUC锁07 ConditionObject分析
查看>>
图像处理实用工具
查看>>
CentOS 7 学习(一) 配置LAMP和Nginx
查看>>
sql临时表,表变量,CTE,游标使用方法
查看>>
路由器和交换机的区别
查看>>
pycharm 的安装及selenium环境的搭建
查看>>
ImageSource使用心得(转)
查看>>
STM32用JLINK 烧写程序时出现NO Cortex-m device found in JTAG chain现象和解决方案
查看>>
背包问题
查看>>
关于spring的bean
查看>>
网络安全系列 之 SQL注入学习总结
查看>>
实验一
查看>>