The specifics discussed here revolve around the workings of inheritance in ES5 and earlier versions. The prototype
property is crucial as it defines functions that can be used by class members.
If you were to directly link them, any functions added to Teacher
would also impact Person
, like so:
Teacher.prototype = Person.prototype;
Teacher.prototype.name = function(...) { }
This linking causes the name()
function to affect Person
as well. By using Object.create()
, a cloning operation occurs, making the two prototypes independent.
The scenario changes significantly in ES6, which offers a cleaner approach:
class Teacher extends Person {
name() {
// ...
}
}
Although this still results in the same Teacher.prototype
, the process is streamlined and simplified, enhancing the structure of classes.