当前位置:网站首页>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语言ggplot2可视化:基于aes函数中的fill参数和shape参数自定义绘制分组折线图并添加数据点(散点)、使用theme函数的legend.position函数配置图例到图像右侧
- STL中list实现
- R语言时间序列数据的平滑:使用KernSmooth包的dpill函数和locpoly函数对时间序列数据进行平滑以消除噪声
- 使用较广泛的安全测试工具有哪些?
- RPA助你玩转抖音,开启电商运营新引擎
- Shell脚本实现多选DNS同时批量解析域名IP地址(新更新)
- 理解JS的三座大山
- Shell script realizes multi-select DNS simultaneous batch resolution of domain name IP addresses (new update)
- 转转反爬攻防战
- 牛客网项目2.7开发注册功能 报错This application has no explicit mapping for /error......
猜你喜欢

瑞吉外卖项目剩余功能补充

Re22:读论文 HetSANN An Attention-based Graph Neural Network for Heterogeneous Structural Learning

第十五章 多线程

李航《统计学习方法》笔记之k近邻法

matlab-day02

DVWA 通关记录 2 - 命令注入 Command Injection

iNFTnews | Seeing the two sides of the metaverse, what is the true Internet and the Internet of value?

日元疲软令游戏机在日本变身“理财产品”:黄牛大赚

The perceptron perceptron of Li Hang's "Statistical Learning Methods" notes

第十六章 协程
随机推荐
【新版干货书】深度伪造 (DeepFakes):创造,检测和影响
瑞萨RZ/G2L处理器详细测评
Do you agree with this view?Most businesses are digitizing just to ease anxiety
Facebook自动化数据分析方案,广告投放省心省力
The love-hate relationship between C language volatile keyword, inline assembly volatile and compiler
软件测试与质量 之白盒测试
日元疲软令游戏机在日本变身“理财产品”:黄牛大赚
DVWA 通关记录 2 - 命令注入 Command Injection
Event 对象,你很了解吗?
This article takes you to understand the commonly used models and frameworks of recommender systems
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
软件测试H模型
The R language uses the ggtexttable function of the ggpubr package to visualize the table data (draw the table directly or add the table data to the image), set the theme parameter to customize the fi
Rust 从入门到精通03-helloworld
TimerTask(addin timer语音)
In the whole development of chi V853 board tried to compile QT test
牛客刷题——剑指offer(第三期)
从零开始入门单片机(一):必会背景知识总结
QT专题:组合会话框和文本编辑器
typeinfo类型支持库学习