当前位置:网站首页>LayaBox---TypeScript---Mixins
LayaBox---TypeScript---Mixins
2022-08-02 10:13:00 【Gragra】
Table of Contents
1. Introduction
In addition to traditional object-oriented inheritance, a popular way to create classes from reusable components is to combine the code of another simple class.You're probably already familiar with mixins and their features in languages like Scala, but it's also very popular in JavaScript.
2.Mixin example
// Disposable Mixinclass Disposable {isDisposed: boolean;dispose() {this.isDisposed = true;}}// Activatable Mixinclass Activatable {isActive: boolean;activate() {this.isActive = true;}deactivate() {this.isActive = false;}}class SmartObject implements Disposable, Activatable {constructor() {setInterval(() => console.log(this.isActive + " : " + this.isDisposed), 500);}interact() {this.activate();}// DisposableisDisposed: boolean = false;dispose: () => void;// ActivatableisActive: boolean = false;activate: () => void;deactivate: () => void;}applyMixins(SmartObject, [Disposable, Activatable]);let smartObj = new SmartObject();setTimeout(() => smartObj.interact(), 1000);// In your runtime library somewherefunction applyMixins(derivedCtor: any, baseCtors: any[]) {baseCtors.forEach(baseCtor => {Object.getOwnPropertyNames(baseCtor.prototype).forEach(name => {derivedCtor.prototype[name] = baseCtor.prototype[name];});});}
3. Understand this example
The code first defines two classes, which will be used as mixins.You can see that each class defines only one specific behavior or function.Later we use them to create a new class that has both functions.
// Disposable Mixinclass Disposable {isDisposed: boolean;dispose() {this.isDisposed = true;}}// Activatable Mixinclass Activatable {isActive: boolean;activate() {this.isActive = true;}deactivate() {this.isActive = false;}}
Let's create a class that combines these two mixins.Here's how it works:
class SmartObject implements Disposable, Activatable {
The first thing you should notice is that instead of using
extends
, you useimplements
.Treat the class as an interface and use only the types of Disposable and Activatable instead of their implementations.This means that we need to implement the interface inside the class.But this is what we want to avoid when using mixins.
We can do this by creating placeholder properties for the property methods that will be mixed in.This tells the compiler that these members are available at runtime.This allows you to use the convenience of mixins, although you need to define some placeholder properties in advance.
// DisposableisDisposed: boolean = false;dispose: () => void;// ActivatableisActive: boolean = false;activate: () => void;deactivate: () => void;Finally, mixins into the defined class to complete the entire implementation part.applyMixins(SmartObject, [Disposable, Activatable]);
Finally, create this helper function to help us do the mixin.It will traverse all the properties on the mixins and copy them to the target, replacing the previous placeholder properties with the real implementation code.
function applyMixins(derivedCtor: any, baseCtors: any[]) {baseCtors.forEach(baseCtor => {Object.getOwnPropertyNames(baseCtor.prototype).forEach(name => {derivedCtor.prototype[name] = baseCtor.prototype[name];})});}
边栏推荐
- 【OpenCV】-霍夫变换
- npm ERR! 400 Bad Request - PUT xxx - Cannot publish over previously published version “1.0.0“.
- Turning and anti-climbing attack and defense
- HikariCP数据库连接池,太快了!
- QT专题:自定义部件
- R语言ggplot2可视化:基于aes函数中的fill参数和shape参数自定义绘制分组折线图并添加数据点(散点)、使用theme函数的legend.position函数配置图例到图像右侧
- R语言ggplot2可视化:使用ggpubr包的ggbarplot函数可视化水平柱状图(条形图)、使用orientation参数设置柱状图转置为条形图
- 你认同这个观点吗?大多数企业的数字化都只是为了缓解焦虑
- logo 图标(php图片加文字水印)
- 迭代器失效问题
猜你喜欢
第十五章 多线程
npm ERR! 400 Bad Request - PUT xxx - Cannot publish over previously published version “1.0.0“.
MySql tens of millions of paging optimization, fast insertion method of tens of millions of data
迭代器失效问题
Turning and anti-climbing attack and defense
用了TCP协议,就一定不会丢包嘛?
只问耕耘,不问收获,其实收获却在耕耘中
Shell script realizes multi-select DNS simultaneous batch resolution of domain name IP addresses (new update)
新“内卷”席卷科技圈,Google CEO 要求 174000 员工提高工作效率!
后管实现面包屑功能
随机推荐
Shell script realizes multi-select DNS simultaneous batch resolution of domain name IP addresses (new update)
MySql千万级分页优化,快速插入千万数据方法
qq邮箱日发5万邮件群发技术(qq邮箱怎样定时发送邮件)
sqlmap安装教程用w+r打开(sqlyog安装步骤)
享年94岁,图灵奖得主、计算复杂性理论先驱Juris Hartmanis逝世
MSYS2 QtCreator Clangd 代码分析找不到 mm_malloc.h的问题补救
8月份的.NET Conf 活动 专注于 .NET MAUI
Use compilation to realize special effects of love
Hongxing, donate another million
Rear tube implements breadcrumb function
软件测试与质量 之白盒测试
带你认识40G单纤双向光模块-QSFP+ BiDi光模块
R语言使用ggpubr包的ggtexttable函数可视化表格数据(直接绘制表格图或者在图像中添加表格数据)、设置theme主题参数自定义表格中表头内容的填充色(使用colnames.style参数)
Event 对象,你很了解吗?
QT专题:事件机制event基础篇
The realization of the list
LayaBox---TypeScript---声明合并
Two-dimensional array piecemeal knowledge sorting
零代码工具推荐---HiFlow
练习-17