Typescript 中 Javascript "prototype" 的替代等价物是什么?

What is the alternative equivalent of Javascript "prototype" in Typescript?

除了 Javascript 提供的函数式编程之外,Typescript 还提供面向对象和通用编程范例。

关键字prototype是一个非常强大但有时也是危险的工具。一般来说,我读到它模拟了Javascript中的继承方面。

所以最好,.ts 中 prototype 的 [最接近] 替代方案是什么?

Typescript provides Object Oriented & Generic programming paradigms apart from Functional programming offered by Javascript.

不,JavaScript 本身提供面向对象和通用的编程模型,也可用于函数式编程风格(但并非专门设计用于支持它,例如 Haskell ).

TypeScript 所做的是:

  • 添加静态类型检查(这是它的主要目的)。
  • 支持通过 TypeScript 编译器尽早采用某些拟议的即将推出的 JavaScript 功能(例如:TypeScript 的编译器很早就支持 class 语法)。
  • 在构造函数上添加一些不太可能成为 JavaScript 功能的额外语法:自动将参数复制到属性。
  • 通过 private 关键字添加 "private" 属性; JavaScript 很快就会有私有属性,但不会使用 TypeScript 使用的语法。

其他都是JavaScript。

当我说 "all" 确实如此时,这并不是要减少这些东西。特别是静态类型检查非常有用,尤其是在大型项目中。

The keyword prototype is a very powerful and sometimes a dangerous tool. In general, I read that it simulates the inheritance aspect in Javascript.

prototype 不是关键字,它只是构造函数上 属性 的名称,它引用将被分配为使用 [=16 创建的对象的原型的对象=] 与这些构造函数一起使用的关键字。

继承未在 JavaScript 中 模拟 。它是 实现的 原型,参见 prototypical inheritance

So preferably, what is the [closest] alternative of prototype in .ts?

那将是:prototype,明确地或间接地通过 class。同样,TypeScript 只是添加了一层静态类型检查和一些小的语法添加。其他一切只是 JavaScript.

Suppose if the answer is inheritance, then how to extend of implement the String kind of interfaces of JS into TS [as there can be lots of abstract methods]?

不太清楚你在这里问什么。您可以扩展字符串:

class MyString extends String {
    // ...
}

...或添加到 String.prototype,理想情况下添加不可枚举的属性,并通过 Object.defineProperty:

分配给它们的函数
Object.defineProperty(String.prototype, "myAddition", {/*...*/});