繁体   English   中英

如何从javascript中的子原型调用父类的构造函数方法?

[英]How to call a constructor method of Parent class from child prototype in javascript?

下面是代码片段,与原型中定义的“eat”方法不同,我在构造函数中定义了“run”方法。 我如何从 B1 的原型访问这个“运行”方法,它是从 A1 继承的。 我的意图是覆盖 B1 中的“运行”方法,在那里我可以使用 A1 的现有“运行”方法。 有什么方法可以实现吗?

 function A1() { this.run = function() { return 'A1 runs'; } }; A1.prototype.eat = function() { return 'A1 eats'; }; function B1() { A1.call(this); }; B1.prototype = Object.create(A1.prototype); //new A1(); B1.prototype.eat = function() { return A1.prototype.eat.call(this) + ',B1 also eats'; } B1.prototype.run = function() { var parentRun = this.constructor.run(); var result = parentRun + ",B1 also runs"; return result; }; var b1 = new B1(); b1.eat(); // It will give 'A1 eats, B1 also eats', which is fine b1.run(); // It will give 'A1 runs', where I want 'A1 runs, B1 also runs'.

编辑 1:我已经更新了代码片段,我已经删除了 console.log 并从每个方法返回了字符串。

最初我没有把我的研究放在这方面。 一些人提到它与重复,我已经浏览过相同的链接,但我的问题完全不同。 我知道如何调用我为“吃”所做的父类原型的方法。

我的查询很简单,
有没有办法访问父类的构造函数('run'方法)的方法,而不是原型的方法('eat'方法)?

三个问题:

  1. A1在实例上run不会返回任何内容,因此调用它会产生undefined

  2. A1run放在instance 上,这意味着在您的b1实例中, B1.prototype.run已被A1直接分配给实例的run遮挡。

  3. this.constructor.run()不是您调用A1版本的方式。

如果你把run放在A1.prototype ,你可以这样做:

function A1() {
}
A1.prototype.eat = function() {
    console.log('A1 eats');
};
A1.prototype.run = function() {
    return 'A1 runs';
};

function B1() {
    A1.call(this);
}
B1.prototype = Object.create(A1.prototype);

B1.prototype.run = function() {
    return A1.prototype.run.call(this) + ",B1 also runs";
};
var b1 = new B1();
console.log(b1.run());

这很丑陋,这就是为什么我写了一个脚本来自动执行当天的超级调用,但这就是在这个级别工作时你如何做到的。

不过,现在你会使用 ES2015 的class语法和super ,如果需要支持过时的 JavaScript 引擎,可以使用BabelTraceur或类似工具进行转译

 class A1 { eat() { console.log("A1 eats"); } run() { return "A1 runs"; } } class B1 extends A1 { run() { return super.run() + ",B1 also runs"; } } var b1 = new B1(); console.log(b1.run());


重新编辑说您希望run在原型上,它必须由A1分配如下:

function A1() {
    this.run = function() {
        return 'A1 runs';
    };
}

这有点麻烦,但你仍然可以做到。 由于我上面提到的原因,在实例上设置run会掩盖您的实例通常会使用的B1.prototype.run 所以要使用它,还要调用A1非原型run ,你必须在实例上替换它。

出于这个原因,定义B1B1 run而不是在B1.prototyperunB1.prototype ,就像A1A1定义它一样,而不是在A1.prototype

function B1() {
    var A1run;
    A1.call(this);
    A1run = this.run;
    this.run = function() {
        return A1run.call(this) + ",B1 also runs";
    };
}

现场示例:

 function A1() { this.run = function() { return 'A1 runs'; }; } A1.prototype.eat = function() { console.log('A1 eats'); }; function B1() { var A1run; A1.call(this); A1run = this.run; this.run = function() { return A1run.call(this) + ",B1 also runs"; }; } B1.prototype = Object.create(A1.prototype); var b1 = new B1(); console.log(b1.run());

但如果你真的,真的希望它(至少部分)在B1.prototype

function B1() {
    var A1run;
    A1.call(this);
    A1run = this.run;
    this.run = function() {
      return A1run.call(this) + B1.prototype.run.call(this);
    };
}
// ...
B1.prototype.run = function() {
    return ",B1 also runs";
};

现场示例:

 function A1() { this.run = function() { return 'A1 runs'; }; } A1.prototype.eat = function() { console.log('A1 eats'); }; function B1() { var A1run; A1.call(this); A1run = this.run; this.run = function() { return A1run.call(this) + B1.prototype.run.call(this); }; } B1.prototype = Object.create(A1.prototype); B1.prototype.run = function() { return ",B1 also runs"; }; var b1 = new B1(); console.log(b1.run());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM