Arrays 在Typescript中扩展数组
如果我尝试在以下位置执行此操作: 这是行不通的<代码>foo未定义。如果我查看生成的代码:Arrays 在Typescript中扩展数组,arrays,typescript,oop,Arrays,Typescript,Oop,如果我尝试在以下位置执行此操作: 这是行不通的foo未定义。如果我查看生成的代码: var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array &a
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var Test = /** @class */ (function (_super) {
__extends(Test, _super);
function Test(items) {
return _super.apply(this, items) || this;
}
Test.prototype.foo = function () { console.log('foo'); };
return Test;
}(Array));
var t = new Test([]);
t.foo();
函数返回\u super.应用
的结果,而不是此
。因此,我最终得到的是一个数组,而不是从数组继承的测试。如果我换这条线
return _super.apply(this, items) || this;
借
它按预期工作。由于数组的特殊性质,这可能是Typescript的错误吗?或者我的实现有什么问题吗?可能是因为
foo
方法返回void
,因此没有任何内容可供console.log使用?你能试着调用t.foo()
?您还缺少了new Test()
构造函数的输入参数,我想……对不起,我修改了测试代码以使问题更清楚,结果把它搞糟了。修好了。看:还有
return _super.apply(this, items) || this;
_super.apply(this, items)
return this;