0

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

T J
  • 42,762
  • 13
  • 83
  • 138
3gwebtrain
  • 14,640
  • 25
  • 121
  • 247

3 Answers3

1

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;
ucdream
  • 691
  • 1
  • 7
  • 18
  • 1
    "in some scenarios, it is necessary to find out the instance's real constructor". You are always creating an instance of the constructor function a.k.a. the "real" constructor. When would this be an issue? – Johan Feb 25 '14 at 12:24
  • 1
    Hi Johan, thanks for the comment. I updated my answer and offered an example which possibly indicates the scenario of 'exact constructor'. – ucdream Feb 25 '14 at 14:35
0

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.

spassvogel
  • 3,479
  • 2
  • 18
  • 23
0

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();
Johan
  • 35,120
  • 54
  • 178
  • 293
  • I would like to know, what is the use of this line "Ferari.prototype.constructor = Ferari;" - can you explain please? – 3gwebtrain Feb 25 '14 at 11:09
  • 1
    @3gwebtrain Personally I haven't find any uses for it. I just set it in case someone else expects it to be there. Even if you don't set it, the actual constructor function will always be `Ferari` i.e. it doesn't get changed to `Car`. If it would change, it would be much more important to set it. Not sure if that made any sense. Related: http://stackoverflow.com/questions/12622137/what-is-the-constructor-property-really-used-for – Johan Feb 25 '14 at 11:17