Javascript“原型"的替代等价物是什么?在打字稿? [英] What is the alternative equivalent of Javascript "prototype" in Typescript?

查看:44
本文介绍了Javascript“原型"的替代等价物是什么?在打字稿?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Typescript 提供面向对象的 &Javascript 提供的函数式编程之外的通用编程范式.

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

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

解决方案

Typescript 提供面向对象的 &Javascript 提供的函数式编程之外的通用编程范式.

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

TypeScript 所做的就是:

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

其他一切都是 JavaScript.

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

<块引用>

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

prototype 不是关键字,它只是构造函数上的属性名称,该属性引用将被分配为使用 new 创建的对象的原型的对象code> 与这些构造函数一起使用的关键字.

继承不是在 JavaScript 中模拟.它通过原型实现,参见原型继承.><块引用>

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

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

<块引用>

假设答案是继承,那么如何将implement的String类接口的extend JS的接口转换成TS[因为可以有很多抽象方法]?

你在这里问的不是很清楚.您可以扩展字符串:

class MyString extends String {//...}

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

Object.defineProperty(String.prototype, "myAddition", {/*...*/});

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

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

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

解决方案

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

No, JavaScript itself offers an object-oriented and generic programming model and can be used in the functional programming style as well (but isn't specifically designed to support it, like Haskell).

All TypeScript does is:

  • Add static type checking (this is its main purpose).
  • Support early adoption of certain proposed upcoming JavaScript features via the TypeScript compiler (for instance: TypeScript's compiler supported class syntax very early on).
  • Add some extra syntax on constructors which is unlikely ever to become a JavaScript feature: automatically copying parameters to properties.
  • Add "private" properties via a private keyword; JavaScript will have private properties soon, but not using the syntax TypeScript uses.

Everything else is JavaScript.

When I say "all" it does, that's not to diminish those things. Static type checking, in particular, can be extremely useful, particularly in large projects.

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 isn't a keyword, it's just the name of a property on constructor functions that refers to the object that will be assigned as the prototype of object created with the new keyword used with those constructors.

Inheritance isn't simulated in JavaScript. It's implemented with prototypes, see prototypical inheritance.

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

That would be: prototype, either explicitly or indirectly via class. Again, TypeScript just adds a layer of static type checking and a couple of minor syntax additions. Everything else is just 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]?

It's not very clear what you're asking here. You can either extend string:

class MyString extends String {
    // ...
}

...or add to String.prototype, ideally adding non-enumerable properties with functions assigned to them via Object.defineProperty:

Object.defineProperty(String.prototype, "myAddition", {/*...*/});

这篇关于Javascript“原型"的替代等价物是什么?在打字稿?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆