0

I have the following code:

      const Person = function (firstName, birthYear) {
  this.firstName = firstName;
  this.birthYear = birthYear;
};

Person.prototype.calcAge = function () {
  console.log(2037 - this.birthYear);
};

const Student = function (firstName, birthYear, course) {
  Person.call(this, firstName, birthYear);
  this.course = course;
};

// Linking prototypes
Student.prototype = Object.create(Person.prototype);

when i'm printing to console :

Student.prototype

I'm getting an empty object {} When i'm printing to console:

Student.prototype.proto

shouldn't i get undefined here??

Attached Screenshot enter image description here

Eitanos30
  • 1,331
  • 11
  • 19
  • 2
    I don't see what `.property` and `.typo` have to do with the above code? Also notice that `Student != Student.prototype`. – Bergi Jan 02 '21 at 20:21
  • @Bergi, this is the code i have written when encounter this issue. Isn't it possible to refer to the situation? – Eitanos30 Jan 02 '21 at 20:25
  • No, in that code `Student.property` is not an empty object, and accessing `Student.property.typo` throws an *`Uncaught TypeError: Cannot read property 'typo' of undefined`* not something about "*not an empty object*". Can you [edit] the question to include a [mcve] please? – Bergi Jan 02 '21 at 20:30
  • @Bergi, i have added a screenshot exactly with the code i wrote in my post.. the screenshot includes the outputs.. I don't see any exception – Eitanos30 Jan 02 '21 at 20:33
  • That's `Student.prototype`, not `Student.property` – Bergi Jan 02 '21 at 20:34
  • "*How is it possible that an empty object can have a property*" - it doesn't have an *own* property. It **inherits** that property from its prototype - welcome to OOP :-) – Bergi Jan 02 '21 at 20:36
  • @Bergi, but he has the `prototype` property.. So why it is being inherited? can you please describe who inherit from who? – Eitanos30 Jan 02 '21 at 20:38
  • The `Student` function object has a `.prototype` property, yes. That `Student.prototype` object has no properties, but it does inherit (e.g. `calcAge`) from `Person.prototype` (since that's how you `Object.create`d it). – Bergi Jan 02 '21 at 20:46
  • See also the diagram in https://stackoverflow.com/questions/65534445/what-is-the-proto-value-of-an-object-that-is-being-created-by-constructor – Bergi Jan 02 '21 at 20:47
  • If you meant `__proto__` not `__typo__`, see https://stackoverflow.com/q/9959727/1048572 – Bergi Jan 02 '21 at 20:57
  • @Bergi, can't understand no matter what i do... The only solution is to leave this language :( i'm broken.. and yes i meant to __proto__ – Eitanos30 Jan 02 '21 at 21:03
  • You might want to ask your lecturer then. StackOverflow is the wrong platform for a tutorial on prototypical inheritance. – Bergi Jan 02 '21 at 21:04
  • I have fixed my post.. really had there terrible mistakes.. the only thing i can't understand it is how an empty object Student.prototype can have a value for Student.prototype.__proto__, shouldn't it be equal to undefined`? – Eitanos30 Jan 02 '21 at 21:16
  • 1
    [`__proto__`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/proto) is a getter inherited from `Object.prototype`. Also it's displayed specially in the console as the prototype chain link. – Bergi Jan 02 '21 at 21:25

0 Answers0