简体   繁体   English

可以在JS中使用Abstract类吗?

[英]Possible to have Abstract class in JS?

I have come across prototype inheritance in javascript. 我在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 really doesn't have anything like this. JavaScript真的没有这样的东西。 As you said, JavaScript is prototype-oriented . 正如您所说,JavaScript是面向原型的


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 . Astractiondecoupling 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. 这些允许你做各种好事,比如使用(作为参数)和生成(返回)具有抽象类型的值的写方法 - 至少满足关于其方法和字段的某些契约的对象。

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. 尝试将类型为Foo的对象传递给只接受Bar *类型对象的方法,然后您将收到编译器警告。

And now for the actual explanations 现在进行实际解释

  1. JavaScript will let you pass any object to any function. JavaScript将允许您将任何对象传递给任何函数。 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. JavaScript变量没有显式类型。 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. 在JavaScript中,函数不会被卡在对象中 - 它们是语言的一等公民,并且可以像对象一样使用。 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. 因此,虽然您无法在创建后真正更改对象的prototypeconstructor (读取: new ),但您可以更改对象的属性。
  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. 与点#2类似,无法保证函数返回的内容,甚至无法保证函数将返回任何值。 Functions don't have an explicitly-declared signature in JavaScript in the way that they do in many OO languages. 函数在JavaScript中没有显式声明的签名,就像它们在许多OO语言中那样。

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. 听起来你正试图将强类型,面向对象的语言的相对刚性强加到JavaScript的“轻松,顺其自然”的动态类型系统中。 IMHO, that's not a great idea. 恕我直言,这不是一个好主意。 Maybe you could explain the actual problem you're trying to solve? 也许你可以解释一下你想要解决的实际问题?

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. 我确定那里至少有一种语言( OCaml? )完全抛弃了我的逻辑。 Throw me some feedback. 给我一些反馈。


*Assuming Foo isn't a subtype of Bar , of course. *当然,假设Foo不是Bar的子类型。

...but only at runtime, so it's really no more of a guarantee than what you already get with JavaScript's type system. ...但仅限于运行时,因此它实际上不再是您使用JavaScript类型系统所获得的保证。

thus possibly 因此可能

Javascript doesn't support it out of the box, but it is easy to simulate the desired semantics. Javascript不支持开箱即用,但很容易模拟所需的语义。 If you have a base 'class' that you want to be abstract, put the method(s) that is (are) abstract on it, and have them throw an error. 如果你有一个想要抽象的基类'class',那就把它上面抽象的方法放在上面,让它们抛出一个错误。 If the user extends your 'class' and does not provide an implementation, the error will be thrown. 如果用户扩展了“类”并且未提供实现,则将引发错误。 For example, you can do 例如,你可以做到

function Thing() {...}
Thing.prototype.abstractMethod = function() {
    throw 'You must implement abstractMethod';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM