[英]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'方法)?
三個問題:
A1
在實例上run
不會返回任何內容,因此調用它會產生undefined
。
A1
將run
放在instance 上,這意味着在您的b1
實例中, B1.prototype.run
已被A1
直接分配給實例的run
遮擋。
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 引擎,可以使用Babel或Traceur或類似工具進行轉譯:
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
,你必須在實例上替換它。
出於這個原因,定義B1
在B1
run
而不是在B1.prototype
上run
是B1.prototype
,就像A1
在A1
定義它一樣,而不是在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.