首页 新闻 赞助 找找看

js 继承 调用父类问题

0
悬赏园豆:30 [待解决问题]
public class parent
{
     public virtual void process()
     {
     }
}

public class child:parent
{
     public override process()
     {
            base.process();
     }

}

在c#中 为了避免写重复代码可以通过 base.方法名去调用父类的方法
那么在js中继承的情况 该如何去调用父类的方法 求解

Parent= function () {

}
Parent.prototype.process = function(){

}

Child= function () {
    Parent.call(this);
}
Child.prototype = new Parent();
Child.prototype.process = function(){
     // 这里怎么写才能调用父类的方法
}

 

小眼睛老鼠的主页 小眼睛老鼠 | 老鸟四级 | 园豆:2731
提问于:2014-10-20 10:05
< >
分享
所有回答(2)
0

谷歌一下, “js作用域”。

Echo.Liu | 园豆:385 (菜鸟二级) | 2014-10-20 16:58
0

我想到一个方法是这样:

    var Parent= function () {

    };

    Parent.prototype.process = function(){
        alert('parent method');
    };

    var Child= function () {
        Parent.call(this);
    };

    Child.prototype = new Parent();
    Child.prototype.process = function(){
         this.__proto__.process();
         alert('child method');
    };

    var childVar = new Child();
    childVar.process();

就是有点担心__proto__是不是所以的浏览器都能认的。

会长 | 园豆:12401 (专家六级) | 2014-10-21 10:31

我上网查了下,可以这样写,完美解决你的问题:

    var Parent= function () {

    };

    Parent.prototype.process = function(){
        alert('parent method');
    };

    var Child= function () {
        Parent.call(this);
    };

    Child.prototype = new Parent();
    Child.prototype.process = function(){
         this.constructor.prototype.process();
         alert('child method');
    };

    var childVar = new Child();
    childVar.process();
支持(0) 反对(0) 会长 | 园豆:12401 (专家六级) | 2014-10-21 10:37
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册