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?

5
  • @OliCharlesworth all those answers use ES3 mechanisms (and are ugly!) Commented Nov 24, 2011 at 1:14
  • @Raynos: That was just the first duplicate question I found. There are plenty of others; the OP can just use the search box... Commented Nov 24, 2011 at 1: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. Commented Nov 24, 2011 at 1: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. Commented Nov 24, 2011 at 1: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. Commented Apr 8, 2019 at 20:54

2 Answers 2

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!

Sign up to request clarification or add additional context in comments.

5 Comments

"By the way: what do classes have to do with OOP practices? Hint: it's OOP, not COP!" <-- love that
+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.
By the way: the word "probably" is important. See the Treaty of Orlando for exactly why I wrote "probably" and not e.g. "always".
Without classes (and interfaces), how do you implement all the well known and taught design patterns that requires them in JS?
@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.
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.