I came across this slide: http://www.slideshare.net/stoyan/javascript-patterns#postComment
at page 35:
Option 5 + super + constructor reset
function inherit(C, P) {
var F = function(){};
F.prototype = P.prototype;
C.prototype = new F();
C.uber = P.prototype;
C.prototype.constructor = C; // WHY ???
}
I don't get it. Can anybody please explain what the last line for ?
C.prototype.constructor = C; // WHY ???
Thanks
A constructor is a special function that creates and initializes an object instance of a class. In JavaScript, a constructor gets called when an object is created using the new keyword. The purpose of a constructor is to create a new object and set values for any existing object properties.
A constructor is used to create a new object and set values for existing object properties. The Error() constructor in JavaScript is used to create new error objects. Error objects are thrown when runtime errors occur. The Error object can also be used as a base object for user-defined exceptions.
What Does Constructor Mean? A constructor is a special method of a class or structure in object-oriented programming that initializes a newly created object of that type. Whenever an object is created, the constructor is called automatically.
A Default Constructor is created automatically by JavaScript if you have not added a constructor method in a particular class. However, if you want to perform any specific operation while creating a class object, you can explicitly define a default constructor method.
This gives an explanation http://phrogz.net/JS/Classes/OOPinJS2.html
In particular
Cat.prototype = new Mammal(); // Here's where the inheritance occurs
Cat.prototype.constructor=Cat; // Otherwise instances of Cat would have a constructor of Mammal
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With