1

From what I read, in JS, the prototype refers to the Object that its constructor functions refer to.

So, let's say,

function Dog(name){
    this.name = name;
}
Dog.prototype.bark = function(){alert("Woof");}

That means now, the Dog's prototype has a bark property.

var dog = new Dog("Doggo");
dog.bark();

Works fine and alerts Woof

However, when I add,

function Cat(name){
    this.name = name;
}

var cat = new Cat("Little kitten");
cat.bark(); //Nothing happens, why?

Both Dog and Cat's prototype point to the same Object.prototype for now (kinda like how Object class is the superclass of all created classes in JAVA, I know prototypes and classes aren't the same but the structure is similar).

So when I am modifying Dog's prototype by adding the bark property, it should reflect in Cat as well because they point to the same Object.prototype which is getting modified? What is wrong with my understanding?

Little_idiot
  • 125
  • 1
  • 8
  • 4
    Your assertion that both functions point to the same prototype object is wrong. They each have distinct prototype objects. – Pointy Jul 14 '20 at 13:21
  • 2
    You can verify that: `console.log(Dog.prototype === Cat.prototype)` (it'll be "false") – Pointy Jul 14 '20 at 13:21
  • 1
    When the constructor function is *defined* it receives a freshly made prototype object. I think that there are abundant examples on the internet on how to correctly set up prototypal inheritance in JavaScript. It has even become easier with the `class...extends` syntax. – trincot Jul 14 '20 at 13:22
  • @Pointy okay, Dog.prototype===Cat.prototype prints false. I am not sure why, because I don't have any other Constructors here and the Objects.prototype is like the base prototype of all JS objects, so shouldn't they point to it? Also, dog.prototype===cat.prototype prints true. Ugh JS is confusing. – Little_idiot Jul 14 '20 at 13:26
  • The prototype of the Object function is *also* a distinct object. Unless you explicitly assign the prototype of one function to another, **every** function has a distinct prototype object. `Object.prototype` has nothing to do with any other function's prototype object. Instances created by constructors do **not** have a `prototype` property unless you make an assignment explicitly. You might want to read some tutorials about JavaScript prototypes and inheritance; it's *nothing* like Java inheritance. – Pointy Jul 14 '20 at 13:29
  • (Actually `Object.prototype` *indirectly* affects other prototypes, because everything has `Object.prototype` on the prototype chain (usually). But it's still a distinct object.) – Pointy Jul 14 '20 at 13:30
  • 2
    @Pointy okay, so in the immediate level, the function prototype refers to the prototype object freshly created at time of function declaration & that's why it Dog.prototype===Cat.prototype prints false? Thanks, I'll read up more on this. – Little_idiot Jul 14 '20 at 13:35
  • @Little_idiot Yes, precisely. – Bergi Jul 14 '20 at 15:42

0 Answers0