Remember to Set the Constructor Property when Changing the Prototype

Im just wondering, by defining a bunch of prototypes for an object or class, you are in essence, mentioning what properties are common to that class or objects of that class. But why then would you not include the constructor as a prototype when defining all the other prototypes, meaning why is it an option? Why isnt it implicityy defined by the js engine, rather than us having to explicitly include it.
programming/remember-to-set-the-constructor-property-when-changing-the-prototype

It’s just a function that creates an object of a specific type: it can’t be defined by the JS engine because it is the core thing that allows what JS now calls classes. Effectively that function is the class: that’s how you define a class, the JS engine can’t do that for you. If you want to pass in arguments to that function, you have to define that function. And you can’t have it defined on the prototype because you need a class to exist before it can have methods defined in its prototype, they can’t be attached to nothing.

So either of these for example, which do exactly the same thing:

function Person (name) {
  this.name = name;
}

Or

class Person {
  constructor(name) {
    this.name = name;
  }
}

(note that the newer additions to the class syntax mean that if you don’t need to pass in arguments, then you don’t need a constructor, but that’s a specific case, in general you’ll want the constructor)

My understanding is that a class definition by saying function Name{…} specifies the properties and methods of the class and that these definitions are not shared among the various objects created. Adding values using prototype specifies class level properties and methods that are shared among all instances. If we do use prototype, then that should imply that a constructor for that class is defined elsewhere, because how could you define prototypes for a class that does not exist? So , what is the point of us having to explicitly mention the constructor in a prototype that already mentions it in the declaration.

Bird.prototype{…} mentions Bird s the class name, so why do we also have to write constructor: Bird? Isnt that a given, a prototype for a class fo Bird would have a constructor for Bird defined also somewhere?

1 Like

Ah you’re talking about the constructor property when you’re redefining the prototype. This bit of the course is trying to explain how JavaScript’s OO works. What you’re doing in this specific task is setting the prototype to a completely new object. The prototype is just another object, and yes, if a “class” (and I need to emphasise, JS does not have classes in the sense of other OO languages, all it has is a way of inheriting properties from other objects) has been defined somewhere, you can freely add prototype methods. It isn’t implicit, it’s explicit – the prototype object is already connected to specific type of object, and objects are freely extensible in JavaScript, so you’re free to add more methods. But if you replace that prototype object with a new object, it’s going to wipe out everything that’s there already, it will lose that link. So in that case, by defining the constructor, you are just explicitly saying "this object is connected to this [constructor] function.

1 Like

I see what youre saying. Thats just the way it is. But

Bird.prototype{
constructor: Bird,
…
}
seems redundant, and is wasted typing, mentioning the same constructor twice. Isnt the goal of ES6 to reduce the number of lines of code, as they did with arrow functions? Maybe, what Im saying is a feature that will be added in the next version of EcmaScript, maybe (fingers crossed)

2 Likes

ES6 adds class syntax, which does reduce the amount of boilerplate when working with inheritance. You simply use class Foo extends Bar and it takes care of setting up the prototype chain for you. The reason FCC has you doing it by hand (to the point of using Object.create) is so you understand how it all works behind the scenes.

1 Like