0

I've been trying to find an answer, and can't seem to find one directly related to this. So I have a Constructor called Person, and to it I assign a prototype method "getFull". My question is, why can't I statically call the "getFull" method (given that getFull does not access "this") with Person.getFull();?

I've done some research on prototypes, and I know that for static methods generally you don't add them to the prototype of the constructor, but instead just add them directly to the constructor (so instead of Person.prototype.getFull, you would normally add Person.getFull), but I'm wondering why this function call won't work. I thought that for any given object property, we first check to see if the object directly contains that property, and if not we recursively check the object's prototype to see if the prototype has the property. Therefore, I assumed that because Person.getFull does not exist, it would default to Person.prototype.getFull.

function Person(name, family) {
    this.name = name;
    this.family = family;
}
Person.prototype.getFull = function() {
    console.log('bla')
};

This is a purely theoretical question, new to javascript (coming from a Java background).

Randy Song
  • 574
  • 1
  • 7
  • 22
  • 2
    `Person` is not an instance of `Person`, it is an instance of `Function`. If you wanted to add a method to a prototype that would be accessed as `Person.foo`, you'd have to add it to `Function.prototype.foo`. – zzzzBov Aug 29 '16 at 17:14
  • "*...we first check to see if the object directly contains that property, and if not we recursively check the object's prototype*" -- You are confusing the `prototype` property of a constructor function and the `[[Prototype]]` internal property of an instance. The prototype-chain search looks at an object's `[[Prototype]]`, which is set at the time the object is created (e.g., by a constructor function or `Object.create(proto)`). A function's `prototype` property is what is used to set a newly-created object's `[[Prototype]]` when that function is called with `new`. – apsillers Aug 29 '16 at 17:19
  • OHHH okay. I was earlier referencing this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain and I have already looked at that link before, but your explanation definitely makes it more clear to me. When we do **Person.prototype.foo**, we're adding to all **Person** instance's prototypes, but the **Person** function itself is not an instance of **Person**, it's an instance of **Function**, because it's merely a constructor. Thanks! – Randy Song Aug 29 '16 at 17:23

0 Answers0