当前位置:网站首页>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];})});}边栏推荐
- R language ggplot2 visualization: use the ggbarplot function of the ggpubr package to visualize the horizontal column chart (bar chart), use the orientation parameter to set the column chart to be tra
- Unknown content monitoring
- 全新荣威RX5,27寸大屏吸引人,安全、舒适一个不落
- LayaBox---TypeScript---迭代器和生成器
- qq邮箱日发5万邮件群发技术(qq邮箱怎样定时发送邮件)
- Smoothing of time series data in R language: smoothing time series data to remove noise using the dpill function and locpoly function of the KernSmooth package
- win10打印服务无法启动(运行时错误automation)
- 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
- 练习-17
- R语言ggpubr包的ggbarplot函数可视化分组柱状图、设置add参数为mean_se可视化不同水平均值的柱状图并为柱状图添加误差线(se标准误差)、position参数自定义分组柱状图分离
猜你喜欢
随机推荐
用正向迭代器封装实现反向迭代器
R语言ggpubr包的ggbarplot函数可视化分组柱状图、设置add参数为mean_se可视化不同水平均值的柱状图并为柱状图添加误差线(se标准误差)、position参数自定义分组柱状图分离
List-based queuing and calling system
只问耕耘,不问收获,其实收获却在耕耘中
LayaBox---TypeScript---高级类型
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
R语言ggpubr包的ggline函数可视化分组折线图、add参数为mean_se和dotplot可视化不同水平均值的折线图并为折线图添加误差线(se标准误差)和点阵图、自定义palette设置颜色
牛客网项目17节生成验证码 刷新验证码一直没反应
链表的实现
QT专题:自定义部件
Why use BGP?
DVWA Clearance Log 2 - Command Injection
适配器模式适配出栈和队列及优先级队列
js防抖函数和函数节流的应用场景
享年94岁,图灵奖得主、计算复杂性理论先驱Juris Hartmanis逝世
食品安全 | 鱼肝油不是鱼油,家有宝宝的注意了
npm ERR! 400 Bad Request - PUT xxx - Cannot publish over previously published version “1.0.0“.
npm ERR! 400 Bad Request - PUT xxx - Cannot publish over previously published version “1.0.0“.
MySql千万级分页优化,快速插入千万数据方法
R language time series data arithmetic operation: use the log function to log the time series data, and use the diff function to calculate the successive difference of the logarithmic time series data









