我对所有扩展抽象类的子类使用依赖注入。
问题是在抽象构造函数类中,我启动了一个我计划在其子项中覆盖的方法,如果需要的话。
我遇到了一个问题,即我注入的依赖项在从 super 启动的覆盖类中不可见。
这是一个代码示例:
abstract class Base {
constructor(view: string) {
this._assemble();
}
protected _assemble(): void {
console.log("abstract assembling for all base classes");
}
}
class Example extends Base {
constructor(view: string, private helper: Function) {
super(view);
console.log(this.helper);
}
public tryMe(): void {
this._assemble();
}
protected _assemble(): void {
super._assemble();
// at first run this.helper will be undefined!
console.log("example assembling", this.helper);
}
}
let e = new Example("hoho", function () { return; })
console.log("So now i will try to reassemble...");
e.tryMe();
所以一个问题的核心是typescript将Example类转译成代码如下:
function Example(view, helper) {
_super.call(this, view);
this.helper = helper;
console.log(this.helper);
}
而不是这个:
function Example(view, helper) {
this.helper = helper;
_super.call(this, view);
console.log(this.helper);
}
如您所见,如果我将this.helper
之前放置_super
在 JavaScript 中,this.helper
将始终在_assemble
. 即使super
会调用该_assemble
函数。
但默认分配给它是在_super
调用之后。所以如果super
类将调用汇编。_assemble
第一次在示例中的覆盖方法中将不可见。
所以我的问题是...
它是一个错误吗?
或者
我不知道什么?
现在我解决了我的问题,只是_assemble
从super
课堂上移除,并且总是从孩子那里调用它。但这只是感觉不对。
Nota Bene:这是编译后的 JavaScript 代码与固定的 JavaScript 代码演示:
TypeScript 通常的输出:
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var Base = (function () {
function Base(view) {
this._assemble();
}
Base.prototype._assemble = function () {
document.write("<p>abstract assembling for all base classes</p>");
};
return Base;
}());
var Example = (function (_super) {
__extends(Example, _super);
function Example(view, helper) {
_super.call(this, view);
this.helper = helper;
console.log(this.helper);
}
Example.prototype.tryMe = function () {
this._assemble();
};
Example.prototype._assemble = function () {
_super.prototype._assemble.call(this);
// at first run this.helper will be undefined!
document.write("<p>example assembling <b/>" + (this.helper) + "</b></p>");
};
return Example;
}(Base));
var e = new Example("test", function () { return "needle"; });
document.write("<p><i>So now i will try to reassemble...</i></p>");
e.tryMe();
TypeScript 修复了 javascript 输出:
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var Base = (function () {
function Base(view) {
this._assemble();
}
Base.prototype._assemble = function () {
document.write("<p>abstract assembling for all base classes</p>");
};
return Base;
}());
var Example = (function (_super) {
__extends(Example, _super);
function Example(view, helper) {
/**
* Slight change, compiled assigning to this BEFORE _super.
*/
this.helper = helper;
_super.call(this, view);
console.log(this.helper);
}
Example.prototype.tryMe = function () {
this._assemble();
};
Example.prototype._assemble = function () {
_super.prototype._assemble.call(this);
// at first run this.helper will be undefined!
document.write("<p>example assembling <b/>" + (this.helper) + "</b></p>");
};
return Example;
}(Base));
var e = new Example("test", function () { return "Needle"; });
document.write("<p><i>So now i will try to reassemble...</i></p>");
e.tryMe();