SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Why type object.prototype.constructor = object?

    Hello everyone, I've been seeing this line of code a lot lately or something similar to it's nature.

    Code:
    Car.prototype = new Vehicle(true, true);
    Car.prototype.constructor = Car;
    What does the last line mean? I can delete this line out and it doesn't seem to change the code in any way. Do you have any practical examples why I should always set an object.prototype.constructor = object? What do I miss? What can't I access if I don't type this line?

    Even if I comment out this line, I get what I want which is that Car inherits the properties of Vehicle, and audi variable gets access to both "classes".

    Here's the rest if you want to know.

    Code:
        function Vehicle(hasEngine, hasWheels) {
        this.hasEngine = hasEngine || false;
        this.hasWheels = hasWheels || false;
    }
    
    Vehicle.prototype.power = function () {  
       console.log("RESCUE");  
    }  
     
     function Car (make, model, hp) {
        this.hp = hp;
        this.make = make;
        this.model = model;
    }
    
    Car.prototype = new Vehicle(true, true);
    //Car.prototype.constructor = Car;
    Car.prototype.displaySpecs = function () {
        console.log(this.make + ", " + this.model + ", " + this.hp + ", " + this.hasEngine + ", " + this.hasWheels);
    }
    var audi = new Car ("Audi", "A4", 150);
    
    audi.displaySpecs();
    // logs: Audi, A4, 150, true, true
    Thanks!

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,197
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Hi ali0020 and welcome to SitePoint,

    To get straight into it you don't need the constructor prototype as all your doing is referencing the new instance of the class which you can use like any other prototype from that point on.

  3. #3
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,313
    Mentioned
    19 Post(s)
    Tagged
    1 Thread(s)
    Ali, when you create a "class", the prototype object comes with an automatic constructor property.

    function Vehicle() {
    }

    console.log(Vehicle.prototype.constructor == Vehicle); // true


    The constructor property allows instances to access the function that created them. One use of this is to test an object's type.

    if (someObj.constructor == Vehicle) {
    }


    But things get a little hairy when we get into inheritance.

    function Car() {
    }

    console.log(Car.prototype.constructor == Car); // true -- so far so good

    Car.prototype = new Vehicle();

    console.log(Car.prototype.constructor == Car); // false -- what happened?


    When we replaced Car's prototype with an instance of vehicle, we lost Car's constructor property, and in its place, we inherit Vehicle's constructor property. This is almost always not what we want, so after we set up inheritance, we then fix the constructor property.

    Car.prototype.constructor = Car;

  4. #4
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by chris.upjohn View Post
    Hi ali0020 and welcome to SitePoint,

    To get straight into it you don't need the constructor prototype as all your doing is referencing the new instance of the class which you can use like any other prototype from that point on.
    Hello Chris! And hm... I see...

    Quote Originally Posted by Jeff Mott View Post
    Ali, when you create a "class", the prototype object comes with an automatic constructor property.

    function Vehicle() {
    }

    console.log(Vehicle.prototype.constructor == Vehicle); // true


    The constructor property allows instances to access the function that created them. One use of this is to test an object's type.

    if (someObj.constructor == Vehicle) {
    }


    But things get a little hairy when we get into inheritance.

    function Car() {
    }

    console.log(Car.prototype.constructor == Car); // true -- so far so good

    Car.prototype = new Vehicle();

    console.log(Car.prototype.constructor == Car); // false -- what happened?


    When we replaced Car's prototype with an instance of vehicle, we lost Car's constructor property, and in its place, we inherit Vehicle's constructor property. This is almost always not what we want, so after we set up inheritance, we then fix the constructor property.

    Car.prototype.constructor = Car;
    May I ask... what's wrong with just leaving it the Car's constructor property as it is? Is there any real world example where
    Code:
    Car.prototype = new Vehicle();
    will hurt my code? What's the loss if I forget to type
    Code:
    Car.prototype.constructor = Car;
    ?

    Yes I can see the property has been reset and we can see this by check it with the console... but... is it bad? What code so

    Thanks.

  5. #5
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,313
    Mentioned
    19 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by ali0020 View Post
    What's the loss if I forget to type
    Code:
    Car.prototype.constructor = Car;
    If you never use the constructor property, nor ever intend to use it, then there's no loss.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •