1

Possible Duplicate:
How do I create an abstract base class in JavaScript?

Is it possible to use OOP practices in javascript and create classes and extend abstract classes?

Community
  • 1
  • 1
Jeff Ward
  • 13
  • 2
  • @OliCharlesworth all those answers use ES3 mechanisms (and are ugly!) – Raynos Nov 24 '11 at 01:14
  • @Raynos: That was just the first duplicate question I found. There are plenty of others; the OP can just use the search box... – Oliver Charlesworth Nov 24 '11 at 01:14
  • @Raynos - if you look at the date of the linked post, it's not unexpected that ES 3 methods dominate. The last line of the linked Crockford article is great: `I now see my early attempts to support the classical model in JavaScript as a mistake.` – RobG Nov 24 '11 at 01:27
  • @RobG I keep forgetting ES5 was only realized in Dec 2009. Still we've made plenty of progress over the last 2.5 years so I would say those techniques are out dated and not very relevant. – Raynos Nov 24 '11 at 01:38
  • ES6 has ````class```` as a reserved keyword which basically (to some extent) behaves like a class in Java - there's the keyword ````constructor````. Behind the scenes of calls to ````class/constructor keywords```` JS is using ````.prototype```` to give properties to a variable. For instance, with prototype chaining you can have inheritance. – rated2016 Apr 08 '19 at 20:54

2 Answers2

4

Is it possible to use OOP practices in javascript

Yes.

and create classes

No, JavaScript doesn't have classes. It is pretty easy to implement them, though, if your design needs them. However, in most cases, if you need classes in JavaScript, you're probably doing something wrong and should rethink your design (or your choice of language, but that is part of the design).

and extend abstract classes?

Since there are no classes: no. But again, you can build them yourself, if you need them. And also again, if you find you need them, you are probably doing something wrong.

By the way: what do classes have to do with OOP practices? Hint: it's OOP, not COP!

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
  • 2
    "By the way: what do classes have to do with OOP practices? Hint: it's OOP, not COP!" <-- love that – Matt Briggs Nov 24 '11 at 01:20
  • 2
    +1 for "if you need classes in JavaScript, you're probably doing something wrong". While javascript can emulate "classic" OO, there really isn't a good reason to do so. The most common is to feel comfortable emulating a familiar pattern. – RobG Nov 24 '11 at 01:29
  • By the way: the word "probably" is important. See the *Treaty of Orlando* for exactly *why* I wrote "probably" and not e.g. "always". – Jörg W Mittag Nov 24 '11 at 03:39
  • Without classes (and interfaces), how do you implement all the well known and taught design patterns that requires them in JS? – rated2016 Apr 08 '19 at 20:57
  • @rated2016: No design pattern requires classes and interfaces. Some design patterns *when implemented in a class-based OO language* require classes. But ECMAScript is not a class-based OO language, so implementing a design pattern in ECMAScript obviously doesn't require classes. For example, the Abstract Factory design pattern *when implemented in Java* requires classes. But, in ECMAScript, it doesn't. In ECMAScript, you would use a constructor function instead. – Jörg W Mittag Apr 09 '19 at 08:58
1

Is it possible to use OOP practices in javascript

In JavaScript you can inherit from objects.

var Base = {
  ...
};

var Extension = Object.create(Base, {
  ...
});

create classes

There is no notion of class. There is only a notion of prototypical inheritance and a prototype relationship.

extend abstract classes?

Depends, define abstract

Article about ES5 OO

Raynos
  • 166,823
  • 56
  • 351
  • 396