0

I was following a video tutorial on JS inheritance, I stuck on the last 2 line of code below :

http://i.imgur.com/A9DDMHL.jpg

assume above there is an Animal class, and we want to use the properties and methods.

I seem to get the second last : Cat.prototype = new Animal(). is it saying assigning a new animal object to cat's prototype? but I cannot understand the last line of code.

3 Answers 3

2

I would recommend reading this article

The prototype property provides the object that methods and properties will be inherited from when you create an object via the Cat constructor function. Whereas constructor is nothing more than a regular property which is typically used to identify the constructor function used to create an object.

Sign up to request clarification or add additional context in comments.

2 Comments

I see! you cant do new Cat() yet because if we do so, we missed all the properties and methods from animal. right?
If you read the reference article, you will find that the constructor property doesn't perform any function, you can do what you like with it!
0

Cat.prototype = new Animal() means that the Cat class is inheriting the methods and properties of the Animal class, most likely because Cat is an Animal, and reusing methods from the Animal class in Cat class wastes time.

Cat.prototype.constructor = Cat is just making the constructor of the Cat class the constructor you just made above for all Cat objects.

4 Comments

This is just totally wrong. Stop speaking about class and superclass for a non class oriented language.
Look at this: ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf 4.2.1 : [ECMAScript does not use classes such as those in C++, Smalltalk, or Java] even if a teenager wrote the opposite on a public wiki.
This question is about javascript, not ECMAScript.
0

First of all, JavaScript is not a class based language. It doesn't know about the concept of classes, but it knows about objects and the prototype chain and with those two components, we can simulate class-like behavior and implement inheritance.

But since this all is kind of a workaround, we sometimes have to put extra effort in it, in order to make it work. And this is where your last line Cat.prototype.constructor = Cat comes in.

The prototype property of every function has a property called constructor which points to the function itself. So when you use the function as a constructor, the internal [[prototype]] object has the constructor property as well.

function Cat() {
}

var cat = new Cat();
console.log(Cat.prototype.constructor); //Cat
console.log(cat.constructor); //Cat

But, if you apply your inheritance pattern, you override the complete prototype property of Cat with all its properties, including the constructor property.

function Cat() {
}

Cat.prototype = new Animal();

var cat = new Cat();
console.log(Cat.prototype.constructor); //Animal
console.log(cat.constructor); //Animal

To fix this, we have to set the constructor property manually:

function Cat() {
}

Cat.prototype = new Animal();
Cat.prototype.constructor = Cat;

var cat = new Cat();
console.log(Cat.prototype.constructor); //Cat
console.log(cat.constructor); //Cat

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.