【第8篇】TypeScript的Mixin案例代码详解
来源:互联网 发布:六轴机械手臂 算法 编辑:程序博客网 时间:2024/06/06 03:24
9.1Mixin使用
Ts代码
/**
* Mixin使用.
*
*随着传统的面向对象的层次结构,从可重用的组件建立类的另一种流行的方式是通过简单的组合部分类来构建他们。
*你可能熟悉混入或性状比如Scala语言的理念,模式也达到了JavaScript的一些社区人气
*/
// Disposable Mixin(一次性)
class Disposable {
isDisposed: boolean;
dispose() {
this.isDisposed = true;
}
}
// Activatable Mixin(激活混入)
class Activatable {
isActive: boolean;
activate() {
this.isActive = true;
}
deactivate() {
this.isActive = false;
}
}
//SmartObject类实现Disposable与Activatable类
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])
var 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];
})
});
}
Js代码
/**
* Mixin使用.
*
*随着传统的面向对象的层次结构,从可重用的组件建立类的另一种流行的方式是通过简单的组合部分类来构建他们。
*你可能熟悉混入或性状比如Scala语言的理念,模式也达到了JavaScript的一些社区人气
*/
// Disposable Mixin(一次性)
var Disposable = (function () {
function Disposable() {
}
Disposable.prototype.dispose = function () {
this.isDisposed = true;
};
return Disposable;
})();
// Activatable Mixin(激活混入)
var Activatable = (function () {
function Activatable() {
}
Activatable.prototype.activate = function () {
this.isActive = true;
};
Activatable.prototype.deactivate = function () {
this.isActive = false;
};
return Activatable;
})();
//SmartObject类实现Disposable与Activatable类
var SmartObject = (function () {
function SmartObject() {
var _this = this;
// Disposable
this.isDisposed = false;
// Activatable
this.isActive = false;
setInterval(function () { return console.log(_this.isActive + " : " + _this.isDisposed); }, 500);
}
//相互作用
SmartObject.prototype.interact = function () {
this.activate();
};
return SmartObject;
})();
applyMixins(SmartObject, [Disposable, Activatable]);
var smartObj = new SmartObject();
setTimeout(function () { return smartObj.interact(); }, 1000);
////////////////////////////////////////
// In your runtime library somewhere
//在您的运行时库的地方
////////////////////////////////////////
function applyMixins(derivedCtor, baseCtors) {
baseCtors.forEach(function (baseCtor) {
Object.getOwnPropertyNames(baseCtor.prototype).forEach(function (name) {
derivedCtor.prototype[name] = baseCtor.prototype[name];
});
});
}
- 【第8篇】TypeScript的Mixin案例代码详解
- 【第4篇】TypeScript类的详解案例代码使用。
- 【第5篇】TypeScript块module的案例代码详解
- 【第6篇】TypeScript函数function的案例代码详解
- 【第7篇】TypeScript泛型的案例代码详解
- 【第9篇】TypeScript声明合并Merging 案例代码详解
- 【第11篇】最全的中文TypeScript入门指南详解案例教程与代码
- 【第10篇】TypeScript类型比较《Type Compatibility》 案例代码详解
- 【第2篇】TypeScript - 基本类型详解
- 【第3篇】TypeScript接口使用
- 详解Typescript中继承的实现
- PHP的Mixin实现
- Jade的mixin用法
- React Mixin 的介绍
- linear-gradient的mixin
- dojo的dojo.mixin
- 带参数的mixin
- 作为函数的mixin
- 【第7篇】TypeScript泛型的案例代码详解
- 【Java设计模式】· 访问者模式(Visitor Pattern)
- 排序算法 C++代码实现
- POJ 3415
- ajax jsonp跨域乱码解决方案
- 【第8篇】TypeScript的Mixin案例代码详解
- 从jQuery里面看变量与作用域
- [精彩] 关于mysql内存溢出,请高手支招
- C++类对象创建过程揭密
- iOS中的GCD多线程
- 设置导航栏上的返回按钮
- HDU 1863 畅通工程 kruskal算法 最小生成树
- 【第9篇】TypeScript声明合并Merging 案例代码详解
- 关于mysql自增id的获取和重置