Prototype VS Inheritance (Basic)

Hi guys,

I’m a little confused with these two.
Okay here is my understanding.

Prototype - extend an object, just like in php you extend a class.
Inheritance - reuse existing codes, by copying or by reference.

Sometimes I’m thinking they are just the same.
Can someone give more advice.

Thanks in advance.

When speaking about the prototype, you are focusing on the parent object (or further parents) itself.
With inheritance, you are focusing instead on the something further down from the parent that inherits from the further up.

For example:


function Cat() {

}
Cat.prototype.speak = 'Meow';

this.cat = new Cat();
this.cat.speak; // Meow

this.snagglepuss = new Cat();
this.snagglepuss.speak = 'Heavens to Murgatroyd!';

this.snagglepuss.speak // Heavens to Murgatroyd!

The Cat constructor function has a prototype property called speak. That speak property will be accessible to all objects created from that constructor.
The cat object inherits the speak property from the Cat constructor.

If the cat object though defines a different property though, then that will be used instead. Searching only goes up the prototype chain until the first matching property is found.

Please paste the above code in to the Object Playground, and you can see a fancy diagram of how things are structured.

@paul_wilkins ;

Thanks dude, I think I need more practice.

“Inheritance” is an abstract idea or feature. “Prototype” is a concrete implementation of that idea.