Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript constructor reset: What is it?

Tags:

javascript

oop

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

like image 508
Sake Avatar asked Apr 11 '10 10:04

Sake


People also ask

What does JavaScript constructor do?

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.

What is constructor error?

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 is this constructor?

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.

What is default constructor in JavaScript?

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.


1 Answers

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 
like image 52
Jonathan Avatar answered Sep 28 '22 21:09

Jonathan