Which inherit method is recommended?

When I searched the web which showed the JavaScript inheritance have other ways, which is different from the MDN doc.MDN constructor method

Object.defineProperty(Teacher.prototype, 'constructor', {
	value: Teacher,
	enumerable: false,
	writable: true
});

The other method is Teacher.prototype.constructor = Teacher;
So which method is recommended?

The difference is that using the Object.defineProperty function you can specify options like enumerable: false so that the constructor will not appear in ‘for in’ loops

If you need for some reason to iterate the properties of a Teacher object or Teacher.prototype it might make sense to skip the constructor.

Try running this code snippet for both of the options:

let t = new Teacher(...);
for (const property in t) {
  console.log(`${property}: ${t[property]}`);
}
2 Likes

thanks a lot for the recommendation. will try it and see how it works.
may i ask you some additional questions after i do it? thanks

Of course, good luck :slightly_smiling_face:

Gues I would like to invite you to come and participate in our Discord Server

Thanks! I fully understand it!