create-subclass.js
来源:互联网 发布:淘宝直播账号出租平台 编辑:程序博客网 时间:2024/06/05 02:25
create-subclass.js
This is a function to implement inheritance using javascript, which is more concise and elegant than the commonly used inheritance.
这是一个使用javascript实现继承的方法,这个方法比常用的继承写法更精简和优雅(呵呵,我认为)。
repository
github
码云
Use
var Subclass = SuperClass.create({ //attributes }, { //prototype object attributes });
or
var Subclass = SuperClass.create(function(/*arguments*/) { //get arguments return { //attributes } }, { //prototype object attributes });
demo
Element
function Element(ctx, style) { this.ctx = ctx; this.style = style;}Element.prototype = { paint: function () { }, render: function () { this.paint(); }};
Rect inherit the Element
var Rect = Element.create({ type: 'rect'}, { shape: function () { var s = this.style; this.ctx.beginPath(); this.ctx.fillStyle = s.color; this.ctx.rect(s.x, s.y, s.w, s.h); this.ctx.fill(); this.ctx.closePath(); }, paint: function () { this.shape(); }});
TextRect inherit the Rect
var TextRect = Rect.create(function (ctx, style, textStyle) { return { textStyle: textStyle }}, { text: function () { var s = this.style; var ts = this.textStyle; this.ctx.beginPath(); this.ctx.fillStyle = ts.color; this.ctx.font = ts.font; this.ctx.textAlign = ts.textAlign; this.ctx.textBaseline = ts.textBaseline; this.ctx.fillText(ts.text, s.x + ts.x, s.y + ts.y); this.ctx.closePath(); }, paint: function () { this.shape(); this.text(); }});
The equivalent is the following code
Element
function Element(ctx, style) { this.ctx = ctx; this.style = style;}Element.prototype = { paint: function () { }, render: function () { this.paint(); }};
Rect inherit the Element
function Rect() { Element.apply(this, arguments); this.type = 'rect';}Rect.prototype = Object.create(Element.prototype);Rect.prototype.constructor = Rect;Rect.prototype.super = Element;Rect.prototype.shape = function () { var s = this.style; this.ctx.beginPath(); this.ctx.fillStyle = s.color; this.ctx.rect(s.x, s.y, s.w, s.h); this.ctx.fill(); this.ctx.closePath();};Rect.prototype.paint = function () { this.shape();};
TextRect inherit the Rect
function TextRect(ctx, style, textStyle) { Rect.apply(this, arguments); this.textStyle = textStyle;}TextRect.prototype = Object.create(Rect.prototype);TextRect.prototype.constructor = TextRect;TextRect.prototype.super = Rect;TextRect.prototype.text = function () { var s = this.style; var ts = this.textStyle; this.ctx.beginPath(); this.ctx.fillStyle = ts.color; this.ctx.font = ts.font; this.ctx.textAlign = ts.textAlign; this.ctx.textBaseline = ts.textBaseline; this.ctx.fillText(ts.text, s.x + ts.x, s.y + ts.y); this.ctx.closePath();};TextRect.prototype.paint = function () { this.shape(); this.text();};
License
MIT © porky-prince
阅读全文
0 0
- create-subclass.js
- Xcode 8.1 Create NSManagedObject Subclass 编译错误
- Subclass
- subclass
- subclass
- subclass与joined-subclass
- [js]object.create图解
- subclass QDialog
- Subclass QSortFilterProxyModel
- Hibernate joined-subclass和subclass的配置文件
- 【cocos2d-js官方文档】十六、create
- js为什么用工厂模式 Class.create
- 谈谈 Object.create ,JS的原型继承
- prototype.js中的class.create()方法
- create-keyframe-animation-js实现css3动画
- VB的SUBCLASS控件
- 子类化subclass
- MFC之subclass
- .NET C# Tostring() format 格式化字符串大全
- js函数
- JMeter源码集成到Eclipse
- 国内几款接口管理平台,使用体验分析对比;总有一款是你想要的!
- BZOJ4580 DP
- create-subclass.js
- 服务器配置反向代理
- 关于Android Studio 更新到3.0版本以后出现的一些问题以及解决方案
- facebook presto源码解读( part3 )
- 《特征工程三部曲》之二:特征选择
- Mybatis注解
- 1
- Linux 下简单的串口编程
- AGI (Analytical Graphics Inc.)