首页 新闻 会员 周边

javascript 的继承

0
悬赏园豆:10 [已解决问题] 解决于 2013-02-19 22:43

如果有一个类

function ReportItem() {
    this.htmlControl = null; //对应容器

    this.basemouseDown = function (e) //单击事件
    {
        
    }

    this.htmlControl = document.createElement("div");
addEvent(this.htmlControl,"mousedown",this.basemouseDown);
}

function SheetArea ()
{
    ReportItem.call(this);
}

inheritPrototype(SheetArea, ReportItem)
//这里是单击事件的实现
SheetArea.prototype.onmouseDown = function () {
    alert("down!");
}

由于基类里已经对htmlControl上绑定了

basemouseDown,我想当单击的时候,可以通过基类的事件转到 子类的onmousedown方法
MinKong的主页 MinKong | 菜鸟二级 | 园豆:202
提问于:2013-02-19 16:23
< >
分享
最佳答案
0

不明白这个“可以通过基类的事件 转到 子类的onmousedown方法” 的意思

收获园豆:10
Yu | 专家六级 |园豆:12980 | 2013-02-19 16:36

也就是基类的html控件的onmousedown事件已经绑定到了基类的

basemouseDown方法,现在 子类里要重写这个方法,添加上实际内容
MinKong | 园豆:202 (菜鸟二级) | 2013-02-19 17:23

@MinKong: 

SheetArea.prototype.mouseDown = function () {
    alert(
"down!");
}

 

这样不行吗

Yu | 园豆:12980 (专家六级) | 2013-02-19 17:30

@Yu: 我试了一下

function ReportItem() {
    this.htmlControl = null; //对应容器
    
    这个函数的声明要去掉
    //this.basemouseDown = function (e) //单击事件
    //{
    //    
    //}

    this.htmlControl = document.createElement("div");

//这里绑定的函数名出现在 子类里才行
addEvent(this.htmlControl,"mousedown",this.basemouseDown);
}
MinKong | 园豆:202 (菜鸟二级) | 2013-02-19 17:41
function ReportItem() {
    this.htmlControl = null; 

    this.basemouseDown = function (e)
    {
        alert("base down");
    }

    this.htmlControl = $("input");

$(this.htmlControl).bind("mousedown",this.basemouseDown)

}

function SheetArea ()
{
    ReportItem.call(this);
}

SheetArea.prototype.mousedown= function () {
    alert("down!");
}

var d=new SheetArea();

d.mousedown();


 FYI

Yu | 园豆:12980 (专家六级) | 2013-02-19 20:03
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册