0

I created a function called Person, defined below:

function Person(name, age) {
   this.name = name
   this.age = age
}

I then used function as an object function constructor, and created a birthday function in the prototype. What I am confused about is why does the last two lines in the code snippet below print different things to the console?

John = new Person("John", 30)
Person.prototype.birthday = function() {
   this.age++
}
Person.prototype //prints { birthday: [Function (anonymous)] } 
Person.__proto__ //prints {}
  • 1
    https://stackoverflow.com/questions/9959727/proto-vs-prototype-in-javascript#:~:text=prototype%20is%20a%20property%20of,object%2C%20pointing%20to%20its%20prototype. – Erenn Jun 08 '21 at 02:20
  • @paulsm4 I'm mainly confused on when you call prototype and __proto__. I understand prototype is only a property of functions and __proto__ is a property of an object (as an answer is that posts says), but in Javascript, is a function considered an object? – Mason Williams Jun 08 '21 at 02:27
  • 1
    @MasonWilliams `.__proto__` is a de-facto, "hidden" feature that you're not really supposed to interact with. That's why it has that awkard name. `.prototype` is an official part of JavaScript that's intended to be modified. As for your example, `.prototype` refers to the function's own prototype while `.__proto__` refers to the prototype of its "parent" in the inheritance hierarchy. `Person.__proto__ === Function.prototype // true, John.__proto__ === Person.prototype //true` – JLRishe Jun 08 '21 at 03:30

0 Answers0