About Object oriented programming

Why is freeCodeCamp stopped using constructor() and super() and now focuses on prototype?

It hasn’t? The OO section describes how OO works in JS, which is via prototypes. The syntax you’re talking about (class) is sugar on top of that.

class Example () {
  constructor(prop) {
    this.prop = prop;
  }

  method () {
    return this.prop;
  }
}

Is

function Example (prop) {
  this.prop = prop;
}

Example.prototype.method = function () {
  return this.prop;
}

Edit: just to clarify, the class syntax makes it easier to write and read [IMO, but YMMV] OO-style code, but the trade-off is that it hides how things like inheritance actually work in JS. That’s not necessarily a bad thing, but it is important to understand what JS is actually doing. And, further to that, why you often can’t or shouldn’t just translate patterns from OO languages like Java or C# straight into JS (and the class syntax may make it look like you can). It is likely that IRL you won’t write code exactly as it is written in the OO section, that you will use the much more convenient class syntax. But it is the exact same thing.

2 Likes

Stopped? Can you be more specific?

JS has a weird history with object oriented programming (OOP). While it has some features of OOP, it is not truly OOP.

JS has a system of object prototypes and some inheritance. In ES6, there was the introduction of classes (which explicitly use the keywords contructor and super). But as Dan points out that is just “syntactic sugar” that just basically reduces to object prototypes. Classes are discussed in the ES6 section and are used extensively in the React section.

1 Like

actually yes, that’s what I was referring, nooow that you said that, it makes sense, I got confused .

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.