【发布时间】:2019-03-09 18:18:51
【问题描述】:
class Person {
contructor() {
this.someSubclassMember();
}
}
class Student {
contructor() {
super();
this.someSubclassMember.bind(this);
}
someSubclassMember() {
}
}
我知道我可以为 somSubclassMember 定义 protected,但我想从父类迭代子类原型?
这可行吗? 谢谢
PS:我在 coffeescript 中看到了它的可行性。这是coffeescript编译的代码
module.exports = ProviderOS = (function(superClass) {
extend(ProviderOS, superClass);
function ProviderOS() {
this.doInternalGetJobCollection = bind(this.doInternalGetJobCollection, this);
this.doCreateJob = bind(this.doCreateJob, this);
this.doCreateOnetimeJob = bind(this.doCreateOnetimeJob, this);
this.doCreateHourlyJob = bind(this.doCreateHourlyJob, this);
this.doCreateDailyJob = bind(this.doCreateDailyJob, this);
this.doExecuteJob = bind(this.doExecuteJob, this);
this.doGetServerInformation = bind(this.doGetServerInformation, this);
this.getBaseName = bind(this.getBaseName, this);
this.onInit = bind(this.onInit, this);
return ProviderOS.__super__.constructor.apply(this, arguments);
}
在这种情况下,我可以从超类访问子类成员。 但 typescript 需要在访问 this 之前调用 super。
【问题讨论】:
-
您可以将方法声明为抽象,但在构造函数中调用它不是一个好主意。该方法将在派生类构造函数(构造函数的主体和字段初始化)之前运行,从而导致各种奇怪和意外的行为。
标签: typescript class inheritance coffeescript super