javascript – What is "Object Oriented" and what other methods?


I work a lot on AngularJS (JavaScript) and with version 2.0 hitting the door, which will have as its main change the use of ECMAS6 read a lot about Object Oriented Programming. If you can keep the answers in that language, already looking forward (or better to say, based on ECMAS6 ), it would be better.

First question: I've heard a lot about "Object Oriented" programming, what other types of programming are there?

Second question, and most important:

What is Object Oriented Programming? For what purpose is it used? When should it be used? When should it be avoided?

Many of the other answers deal with this subject in a very technical way. I would like a more practical answer, I can learn better if I see the application of theory. So, if anyone can answer that with an actual application of programming, that would be great. That's why I'm asking this question, as I haven't got any practical examples.


I've heard a lot about "Object Oriented" programming, what other types of programming are there?

Imperative, Declarative, Functional, Logic, Dataflow , Function-level , Concurrent, Automated based, Array based,… The list is huge, see the box to the right of this Wikipedia article for an idea.

What is object-oriented programming?

It consists of dividing the system design into small modules – called "objects" – that unite state and behavior (ie data and code that acts on that data). Each object should ideally have limited responsibility ("does just one thing and do it well") and need not (ideally, nor should) be aware of the rest of the objects that make up the system (ie its dependence on other objects should be limited). Finally, an object may have a lifecycle, but the exact sequence in which its various methods will be called is not necessarily defined a priori – in particular, when an object is designed, one does not yet need to have a view of the whole.

A program can use sporadically objects, but the objects are a central aspect is the specific system or language / platform used, then it is said that he is "object oriented" (object oriented, or popular, "object – oriented "). An object-oriented system essentially consists of a set of objects exchanging messages with each other. A message is a method call, basically: if it invokes a specific function of that object, passing arguments, this function acts both on those arguments and on the object's internal state, and finally returns a response to the caller of the function. In the course of this, this method can in turn send messages to other objects, which will also act accordingly.

The object's idea is that it is designed without a view of the whole, and can be included in different systems without alteration (ie reused). A good object is designed separately from any system, and a good system has a modicum of logic of its own other than "instantiating a bunch of objects" and "making them exchange messages with each other". At least that's the theory (in practice, it's hard to see a system that strictly follows this protocol).

For what purpose is it used?

OO techniques have specific purposes: the objects themselves serve to reduce coupling and make the system very modular; inheritance and composition serve to reuse ready-made objects in different circumstances, without modification; polymorphism serves to create "generic" functions that act on similar but not identical data; etc. I can't say if object orientation as a whole has an end, except as a proposal for a logical organization of the code (which is by no means the only possible one, as already seen at the beginning of the answer).

When should it be used? When should it be avoided?

When the benefits it promises are actually realized. When the problem domain does not "fit" well in the OO model, or when another model brings better advantages.

Seriously, I can't answer this generically. Object orientation is a broad concept, which encompasses a series of techniques each with its advantages and disadvantages. Fortunately, there is nothing to force us to follow a single paradigm by iron and fire, and modern languages ​​have incorporated more and more aspects of different paradigms (increasing the tools that the programmer has available to choose from). Thus, it is up to the programmer to analyze case by case whether a certain technique is necessary/desirable or whether it can be left out if the benefit does not justify the cost.

So if anyone can answer that with a real application of programming, that would be great.

Imagine the following situation: "take a number, and create a function from that number, which each time it is invoked returns that same number incremented by N".

An object-oriented solution would be:

class Incremento {
    constructor(x) {
        this.x = x;
    incrementar(n) {
        this.x += n;
        return this.x;

// Uso

var inc = new Incremento(10);
inc.incrementar(2); // 12
inc.incrementar(3); // 15
inc.incrementar(1); // 16 

A non-object oriented solution could be (using first-class closures and functions):

function incremento(x) {
    return function(n) {
        x += n;
        return x;

// Uso

var inc = incremento(10);
inc(2); // 12
inc(3); // 15
inc(1); // 16 
Scroll to Top