可以在JS中使用Abstract类吗? [英] Possible to have Abstract class in JS?

查看:272
本文介绍了可以在JS中使用Abstract类吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在javascript中遇到了原型继承。

I have come across prototype inheritance in javascript.

我想要实现的是方法概述,必须实现/定义继承的时候。

What I wanted to achieve is an outline of methods, which when are inherited must be implemented/defined.

我想知道这是否可能,如果可行,如何。

I would like to know if this is possible and if so, how.

推荐答案

JavaScript真的没有这样的东西。正如您所说,JavaScript是面向原型

JavaScript really doesn't have anything like this. As you said, JavaScript is prototype-oriented.

正如其他答案所说,当然,你可以模拟这个。但我看不出有这么好的理由。为什么面向对象的程序员使用接口和抽象类? Astraction 解耦。这些允许你做各种好事,比如使用(作为参数)和生成(返回)具有抽象类型的值的写方法 - 至少满足关于其方法的某些契约的对象和字段。

As other answers have said, sure, you can simulate this. But I can't see a good reason to do so. Why do object-oriented programmers use interfaces and abstract classes? Astraction and decoupling. These allow you to do all sorts of nice things, like write methods that consume (take as arguments) and produce (return) values that have abstract types - objects that will at least satisfy some contract regarding its methods and fields.

反过来,我们得到其他好东西,比如类型安全的编译时检查。尝试将类型 Foo 的对象传递给只接受 Bar *类型的对象的方法,然后你就会得到编译器警告。

In turn, we get other "nice things" like compile-time checks for type safety. Try to pass an object of type Foo to a method which only accepts objects of type Bar* and you'll get a compiler warning.


  1. JavaScript会让你将任何对象传递给任何函数。你在函数内部使用该对象可能会导致运行时错误 - 但是没有什么能阻止你传递任意参数,甚至是与函数声明的参数数量不同的参数。

  2. JavaScript变量没有显式类型。当你还需要执行手动,显式类型检查时,某些对象有一些方法可以保证有什么意义?

  3. 在JavaScript中,函数不会被困在对象中 - —他们是该语言的一等公民,可以像对象一样使用。所以? 对象的函数可以在运行时更改 您可以向对象添加其他函数,或者从对象中删除它们(或者在原型中隐藏函数)。所以,虽然在创建之后你无法真正改变对象的 prototype 构造函数(阅读: new ),你可以改变对象的属性。

  4. 类似于第2点,没有办法保证函数返回什么,甚至保证函数将返回任何值。函数在JavaScript中没有明确声明的签名,就像它们在许多OO语言中那样。

  1. JavaScript will let you pass any object to any function. What you do with that object inside of the function might cause runtime errors - but nothing will stop you from passing arbitrary arguments, even a different number of arguments than the function declares.
  2. JavaScript variables don't have explicit types. What's the point of "guaranteeing" that some object has some set of methods when you still have to perform manual, explicit type checking?
  3. In JavaScript, functions aren't stuck being subservient to objects — they are first class citizens of the language, and can be used just like objects. So? An object's functions can change at runtime. You can add additional functions to an object, or remove them from an object (or shadow a function in the prototype). So, while you can't really change an object's prototype or constructor after creation (read: new), you can change the object's properties.
  4. Similar to point #2, there is no way to guarantee what a function returns, or even guarantee that a function will return any value at all. Functions don't have an explicitly-declared signature in JavaScript in the way that they do in many OO languages.



你究竟是什么试图做什么?



听起来你正试图将强类型,面向对象语言的相对刚性强加到JavaScript的放松,继续使用 -flow动态类型系统。恕我直言,这不是一个好主意。也许你可以解释一下你想要解决的实际问题?

What are you actually trying to do?

It sounds like you're trying to impose the relative rigidity of strongly-typed, object-oriented languages onto JavaScript's "relaxed, go-with-the-flow" dynamic type system. IMHO, that's not a great idea. Maybe you could explain the actual problem you're trying to solve?

如果这是啰嗦,咆哮或语无伦次,请抱歉。我确定至少有一种语言( OCaml?)完全抛弃了我的逻辑。给我一些反馈。

Sorry if this is long-winded, rant-y, or incoherent. I'm sure there's at least one language out there (OCaml?) that totally throws a wrench in my logic. Throw me some feedback.

*假设 Foo 当然不是 Bar 的子类型。

*Assuming Foo isn't a subtype of Bar, of course.

.. 。但只是在运行时,所以它实际上不再是你用JavaScript类型系统所获得的保证。

...but only at runtime, so it's really no more of a guarantee than what you already get with JavaScript's type system.

因此可能

这篇关于可以在JS中使用Abstract类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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