Use class Syntax to Define a Constructor Function -- help

Tell us what’s happening:
I simply want to know, what does the following mean, it is part of the lesson explanation

This is to be noted, that the class syntax is just a syntax, and not a full-fledged class based implementation of object oriented paradigm, unlike in languages like Java, or Python, or Ruby etc.

Your code so far

function makeClass() {
  "use strict";
  /* Alter code below this line */

  /* Alter code above this line */
  return Vegetable;
const Vegetable = makeClass();
const carrot = new Vegetable('carrot');
console.log(; // => should be 'carrot'

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

Link to the challenge:

I think this should be reworded a bit. Here’s what it’s saying:

In languages like Java and Ruby, you have mechanisms built into the language to use classes. A class is just a useful way to structure code. JavaScript recently adopted a class keyword, but under the hood, it’s not the same as what’s happening with Java and Ruby. Because JavaScript relies on prototypal inhertiance rather than class structures, the keyword actually builds an object with your data and sets its prototype.

It’s a technical difference that will be important for you to learn when you want to master JavaScript, but I wouldn’t get too hung up on it for now.

1 Like

I think @PortableStick said it very well

I just want to emphasize that it’s there really for those that are coming to FCC having already learnt languages that feature full object oriented programming

If it wasn’t said, there’d be a risk of those people assuming certain behaviour that doesn’t map well to what JavaScript is really doing

I would add something to PortableStick’s answer because the technical differences are a concept I’m just starting to learn. I’d like to test some of my own knowledge by answering this question more in depth.

Languages that use “Classical Inheritance” (languages that PortableStick provided examples of above) do not allow new instances of objects to exist without extending an existing class. i.e.:

class Vehicle {}

obj Car extends Vehicle {}

So, the classes and objects derived from a parent class implicitly gain (“inherit”) the properties and methods of their parent.

JavaScript, on the other hand, definitely does not use classical inheritance - try asking your dev tools console:

new Object();

And for me, using the word “delegation” to describe JavaScript’s prototype-based behavior, instead of inheritance, has been useful. Instead of child objects implicitly gaining, or inheriting the data encapsulated inside a parent class, JavaScript objects “delegate” up the prototype chain. Here’s an example that’s helped me:

let foo = {
	a: 1

let bar = Object.create(foo);
bar.a;   // 1
foo.a;  // 1
bar.hasOwnProperty('a');  // false
foo.hasOwnProperty('a');  // true

Here, the interpreter looks at bar.a and returns 1. However, when asked bar.hasOwnProperty('a'), the result is false. This is because, having no success checking bar for a property a, (and returning false) the interpreter looks up the prototype chain to the object the bar is delegating to, and a is defined on foo, so we find that foo.hasOwnProperty('a'); is true

So - long story short - when freeCodeCamp says:

This is to be noted, that the class syntax is just a syntax, and not a full-fledged class based implementation of object oriented paradigm, unlike in languages like Java, or Python, or Ruby etc.

What they mean is the implementation of class in JavaScript is functionally very different than what is actually happening when we think about classical inheritance in another language. JavaScript has its own awesome way of doing things :slight_smile:

JavaScript’s class and inheritance implementation is actually quite similar to Ruby’s, and Python’s too. In both Ruby and Python, a class is itself an object, which means, for example, that you can monkey patch Ruby and Python classes at runtime. Also in both Ruby and Python, inheritance from instance to class to superclass is done by delegation, just like in JavaScript.

JavaScript and Python side-by-side comparison.

cc @gebulmer @d-rh

1 Like