当前位置:网站首页>LayaBox---TypeScript---Mixins
LayaBox---TypeScript---Mixins
2022-08-02 10:01:00 【格拉格拉】
目录
1.介绍
除了传统的面向对象继承方式,还流行一种通过可重用组件创建类的方式,就是联合另一个简单类的代码。 你可能在Scala等语言里对mixins及其特性已经很熟悉了,但它在JavaScript中也是很流行的。
2.混入示例
// Disposable Mixin
class Disposable {
isDisposed: boolean;
dispose() {
this.isDisposed = true;
}
}
// Activatable Mixin
class 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();
}
// Disposable
isDisposed: boolean = false;
dispose: () => void;
// Activatable
isActive: boolean = false;
activate: () => void;
deactivate: () => void;
}
applyMixins(SmartObject, [Disposable, Activatable]);
let smartObj = new SmartObject();
setTimeout(() => smartObj.interact(), 1000);
// In your runtime library somewhere
function applyMixins(derivedCtor: any, baseCtors: any[]) {
baseCtors.forEach(baseCtor => {
Object.getOwnPropertyNames(baseCtor.prototype).forEach(name => {
derivedCtor.prototype[name] = baseCtor.prototype[name];
});
});
}
3.理解这个例子
代码里首先定义了两个类,它们将做为mixins。 可以看到每个类都只定义了一个特定的行为或功能。 稍后我们使用它们来创建一个新类,同时具有这两种功能。
// Disposable Mixin
class Disposable {
isDisposed: boolean;
dispose() {
this.isDisposed = true;
}
}
// Activatable Mixin
class Activatable {
isActive: boolean;
activate() {
this.isActive = true;
}
deactivate() {
this.isActive = false;
}
}
下面创建一个类,结合了这两个mixins。 下面来看一下具体是怎么操作的:
class SmartObject implements Disposable, Activatable {
首先应该注意到的是,没使用
extends
而是使用implements
。 把类当成了接口,仅使用Disposable和Activatable的类型而非其实现。 这意味着我们需要在类里面实现接口。 但是这是我们在用mixin时想避免的。
我们可以这么做来达到目的,为将要mixin进来的属性方法创建出占位属性。 这告诉编译器这些成员在运行时是可用的。 这样就能使用mixin带来的便利,虽说需要提前定义一些占位属性。
// Disposable
isDisposed: boolean = false;
dispose: () => void;
// Activatable
isActive: boolean = false;
activate: () => void;
deactivate: () => void;
最后,把mixins混入定义的类,完成全部实现部分。
applyMixins(SmartObject, [Disposable, Activatable]);
最后,创建这个帮助函数,帮我们做混入操作。 它会遍历mixins上的所有属性,并复制到目标上去,把之前的占位属性替换成真正的实现代码。
function applyMixins(derivedCtor: any, baseCtors: any[]) {
baseCtors.forEach(baseCtor => {
Object.getOwnPropertyNames(baseCtor.prototype).forEach(name => {
derivedCtor.prototype[name] = baseCtor.prototype[name];
})
});
}
边栏推荐
- R语言使用ggpubr包的ggtexttable函数可视化表格数据(直接绘制表格图或者在图像中添加表格数据)、设置theme主题参数自定义表格中表头内容的填充色(使用colnames.style参数)
- The ggline function of the R language ggpubr package visualizes grouped line graphs, the add parameter is mean_se and dotplot to visualize line graphs of different level averages, and adds error bars
- Spearman's correlation coefficient
- R语言ggplot2可视化:基于aes函数中的fill参数和shape参数自定义绘制分组折线图并添加数据点(散点)、使用theme函数的legend.position函数配置图例到图像右侧
- Linux system uninstall, install, upgrade, migrate clickHouse database
- 使用scrapy 把爬到的数据保存到mysql 防止重复
- 行为型模式-模板方法模式
- 带你认识40G单纤双向光模块-QSFP+ BiDi光模块
- 斯皮尔曼相关系数
- 周杰伦新歌发布,爬取《Mojito》MV弹幕,看看粉丝们都说的些啥!
猜你喜欢
瑞吉外卖项目剩余功能补充
RPA助你玩转抖音,开启电商运营新引擎
QT专题:自定义部件
npm ERR! 400 Bad Request - PUT xxx - Cannot publish over previously published version “1.0.0“.
Using the TCP protocol, will there be no packet loss?
Getting Started with SCM from Scratch (1): Summary of Background Knowledge
软件工程国考总结——选择题
李航《统计学习方法》笔记之k近邻法
神通数据库,批量插入数据的时候失败
重磅大咖来袭!阿里云生命科学与智能计算峰会精彩内容剧透
随机推荐
Verilog的随机数系统任务----$random
Do you agree with this view?Most businesses are digitizing just to ease anxiety
如何安装dosbox(pycharm详细安装教程)
The realization of the list
众城优选系统开发功能
Re22:读论文 HetSANN An Attention-based Graph Neural Network for Heterogeneous Structural Learning
Long battery life or safer?Seal and dark blue SL03 comparison shopping guide
typeinfo类型支持库学习
yolov7创新点
HikariCP database connection pool, too fast!
Shell script realizes multi-select DNS simultaneous batch resolution of domain name IP addresses (new update)
【云原生】快出数量级的性能是怎样炼成的?就提升了亿点点
The love-hate relationship between C language volatile keyword, inline assembly volatile and compiler
每日一题练习1-15
超赞!发现一个APP逆向神器!
二维数组零碎知识梳理
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
李航《统计学习方法》笔记之感知机perceptron
软件测试与质量 之白盒测试
Facebook's automated data analysis solution saves worry and effort in advertising