Javascript 从打字稿调用原型函数

Javascript prototype function calling from typescript

我有一个 Angular 应用程序,其中包含多个来自 /app/asset/scripts 的外部 JavaScript 文件,并且在从 <script> 标记调用时工作正常。我正在尝试从 angular(.ts) 调用 JavaScript 函数,但它会引发错误。这是片段:

src/app/asser/scripts/viewer.js

function MyView(x, y) {
  //other stuffs
  this.initView(x, y); <---- error here
}

MyView.prototype.initView = function(x, y) {
  console.log("initView");
}

src/app/viewer/viewer-component.ts

declare var MyView: any;

export class myComponent{
  constructor(){}
  init(){
    this.view = MyView(1000, 800);
  }
}

同时从 JS 调用 initView。它抛出以下错误:

ERROR TypeError: this.initView is not a function
    at MyView (view.js:69)
    at RendererService.push.i2er.RendererService.SetupURL (viewer0.ts:31)

如何解决这个错误?

谢谢

function MyView(x, y) {
  //other stuffs
  this.initView(x, y); <---- error here
}

MyView.prototype.initView = function(x, y) {
  console.log("initView");
}

MyView是一个构造函数。

为了使用 this 你应该用 new 关键字调用它

new MyView(2,2)

我不确定你是否需要declare var MyView: any;