当前位置:网站首页>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];})});}
边栏推荐
- LayaBox---TypeScript---Mixins
- 如何安装dosbox(pycharm详细安装教程)
- Linux system uninstall, install, upgrade, migrate clickHouse database
- QT专题:组合会话框和文本编辑器
- Rust 从入门到精通03-helloworld
- js防抖函数和函数节流的应用场景
- R language ggplot2 visualization: use the ggbarplot function of the ggpubr package to visualize the stacked bar plot, the lab.pos parameter specifies the position of the numerical label of the bar cha
- 练习16-两道模拟题
- Shell脚本实现多选DNS同时批量解析域名IP地址(新更新)
- 食品安全 | 鱼肝油不是鱼油,家有宝宝的注意了
猜你喜欢
Shell script realizes multi-select DNS simultaneous batch resolution of domain name IP addresses (new update)
HikariCP数据库连接池,太快了!
从零开始入门单片机(一):必会背景知识总结
MySql千万级分页优化,快速插入千万数据方法
全新荣威RX5,27寸大屏吸引人,安全、舒适一个不落
MySql tens of millions of paging optimization, fast insertion method of tens of millions of data
第十七章 Excel操作
使用较广泛的安全测试工具有哪些?
瑞萨RZ/G2L处理器详细测评
太帅了!我用炫酷大屏展示爬虫数据!
随机推荐
R language ggplot2 visualization: use the ggtexttable function of the ggpubr package to visualize tabular data (directly draw tabular graphs or add tabular data to images), use tbody_add_border to add
如何封装微信小程序的 wx.request() 请求
Rust 从入门到精通03-helloworld
从零开始入门单片机(一):必会背景知识总结
基于列表的排队与叫号系统
后管实现面包屑功能
LayaBox---TypeScript---Three slash instructions
【术语科普】关于集成工作台那些难懂的词儿,看这篇秒懂!
LayaBox---TypeScript---声明合并
练习-17
Alibaba CTO Cheng Li: Alibaba Open Source History, Concept and Practice
程序员的浪漫七夕
Event 对象,你很了解吗?
The perceptron perceptron of Li Hang's "Statistical Learning Methods" notes
R language ggplot2 visualization: based on the fill parameter and shape parameter in the aes function, custom draw a grouped line chart and add data points (scatter points), use the legend.position fu
R语言ggplot2可视化:使用ggpubr包的ggbarplot函数可视化堆叠的柱状图(stacked bar plot)、lab.pos参数指定柱状图的数值标签的位置,lab.col参数指定数值标
MySql tens of millions of paging optimization, fast insertion method of tens of millions of data
yolov7 innovation point
WPF 截图控件之文字(七)「仿微信」
行为型模式-模板方法模式