在JavaScript中能不能实现自建对象的继承?

jamlu 2003-04-09 10:58:23
在JavaScript中能不能实现自建对象的继承?
...全文
23 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
emu_ston 2003-04-09
  • 打赏
  • 举报
回复
只能模拟继承。
daiboy 2003-04-09
  • 打赏
  • 举报
回复
可以实现
孟子E章 2003-04-09
  • 打赏
  • 举报
回复
Although JavaScript does not support an explicit inheritance operator, you can implement inheritance in JavaScript by having the subclass call the superclass. In the following example, we have two class definitions: superclass() and subclass(). The class subclass() first assigns its superclass() to the inheritFrom() method, and then calls the newly-defined method:

function superclass() {
this.info = alert("Defining the Superclass");
}

function subclass() {
this.inheritFrom = superclass;
this.inheritFrom();
this.info = alert("Overriding the Superclass");
}
To activate the generation of subclass(), click here. This button calls the following function:


function createSubclass() {
var newClass = new subclass();
}
Notice the alert boxes. They show that the info method is first defined in superclass() and then is being overridden in subclass().

87,907

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧