Anu*_*rag 3 javascript oop inheritance prototype interface
我正在研究一个简单的问题来表示一些具有层次结构的类型.有一个数据行包含一些数据,数据可能因行的类型类型而异.简单行可能只有标题和日期,而扩展行可能包含标题,描述,日期和图像.我不是Javascript的新手,但不太了解它继续前进.举一个简单的例子,下面是我将如何用Java编写它:
interface Row {
View getView();
}
class BasicRow implements Row {
private String title;
private String description;
public BasicRow(String title, String description) {
this.title = title;
this.description = description;
}
public View getView() {
// return a View object with title and description
}
}
class ExtendedRow implements Row {
private String title;
private String description;
private Date date;
private Image image;
public ExtendedRow(String title, String description, Date date, Image image) {
this.title = title;
this.description = description;
this.date = date;
this.image = image;
}
public View getView() {
// return a View object with title
// description, date, and image
}
}
Run Code Online (Sandbox Code Playgroud)
这里可以完成的OO改进很少,例如从BasicRow扩展ExtendedRow,只定义新字段并覆盖getView方法.
Javascript没有接口或抽象类,我担心我还没有去思考原型.那么我怎样才能在Javascript中实现像上面例子那样基本的东西,其中有一个基类或一个接口,以及两个从该基类扩展的类,每个类都有自己的特定行为.
任何指针都非常感谢.
看看Douglas Crockford的文章:
Douglas Crockford是Yahoo!的高级JavaScript架构师.他以介绍JavaScript Object Notation(JSON) - 维基百科的工作而闻名
这些文章是必读的,我相信它们会帮助你弄清楚如何构建你的对象.
| 归档时间: |
|
| 查看次数: |
418 次 |
| 最近记录: |