3gwebtrain
3gwebtrain

Reputation: 15293

Javascript what is the use of defining constructor in prototype?

I am looking for a best clarification on my doubt. I do have a function, and from the function i am calling a method, it works fine.

example:

function Car(){
   this.name  = 'car';   
}

function Ferari(){}

Ferari.prototype = new Car;

Ferari.prototype.value = 122;

var fa = new Ferari;

console.log(fa.constructor, fa.name);

from the output in console i am getting my constructor is 'Car'. I changed the constructor as follows:

Ferari.prototype.constructor = Ferari;

now the output in console for constructor is 'Ferari' - it's fine.

  1. can any one tell me what is the use of declaring constructor?
  2. Is that a best practice?
  3. In case if I am not declaring the constructor what is the issue i will be getting?
  4. what are the ways for declaring constructors?

any one detail me the above questions?

Live Demo

Upvotes: 0

Views: 65

Answers (3)

ucdream
ucdream

Reputation: 691

Questions:

  1. can any one tell me what is the use of declaring constructor?
  2. Is that a best practice?
  3. In case if I am not declaring the constructor what is the issue i will be getting?
  4. what are the ways for declaring constructors?

Answers:

[A1] in some scenarios, it is necessary to find out the instance's real constructor, especially inheritance level is deep. e.g. https://github.com/chennanfei/ThinkMVC/blob/master/thinkmvc.js#L737. In a word, to know the exact construtor of instances is helpful for some cases.

[A2] As Johan pointed out,

Ferari.prototype = Object.create(Ferari.prototype);
Ferari.prototype.constructor = Ferari;

[A3] I don't think you will get issues unless you use it obviously.

[A4] Your code indicates the general way. I think following way works, too.

var Cart = function() {...}

To answer Johan's question in the comment, I added more here. (text's size is limited in comment.) here is a little complex case. Suppose the 'class' Base provides a method 'setProtoValue' which allows instances to set/update the properties of their prototype. Classes A and B inherit from Base. When A's instances call the method 'setValue', of course we don't hope affect all B's instances. So we need know the exact constructor of A. In following example, if we don't reset A.prototype.constructor, all Base instances including B will be affected which is unexpected.

var Base = function() {};
Base.prototype.setProtoValue = function(key, value) {
    this.constructor.prototype[key] = value;
};

var A = function() {};
A.prototype = Object.create(Base.prototype);
A.prototype.constructor = A;

var B = function() {};
B.prototype = Object.create(Base.prototype);
B.prototype.constructor = B;

Upvotes: 1

Johan
Johan

Reputation: 35194

If you do Ferari.prototype = new Car;, the Car constructor will be called regardless of whether if you create an instance of Ferari or not.

With that in mind, I suggest the following change:

function Car(){
    console.log('car');
}

function Ferari(){
    Car.apply(this, arguments); // Call the base class constructor manually
    console.log('ferrari');
}

Ferari.prototype = Object.create(Ferari.prototype);
Ferari.prototype.constructor = Ferari;

var fa = new Ferari();

Upvotes: 0

spassvogel
spassvogel

Reputation: 3541

Well, as you can see if you don't declare the constructor to be Ferrari, the superclass constructor (so that's Car) will be used. Unless that's what you want, keep the redeclaration of constructor.

See example:

function Car(){
    console.log('car');
}

function Ferari(){
    console.log('ferrari');
}

Ferari.prototype = new Car;
//Ferari.prototype.constructor = Ferari;

var fa = new Ferari();
console.log(fa.constructor);

Comment the redeclaration of Ferari to see the difference.

Upvotes: 0

Related Questions