Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8607837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:27:35+00:00 2026-06-12T03:27:35+00:00

In trying to understand javascript constructors, I have been looking at this question .

  • 0

In trying to understand javascript constructors, I have been looking at this question.

It seemed to me that I understood it reasonably, but, ironically, when I tried to run similar code, it did not work for me at all.

This is my code

function Car(name) {
    this.Name = name;
    this.Year = 1999;
}

Car.prototype.Drive = function() {
    document.write("My name is '" + this.Name + "' and my year is '" + this.Year + "'. <br />");
};

SuperCar = function () { };
SuperCar.prototype = new Car();

function SuperCar(name) {
    Car.call(this, name);
}

var MyCar = new Car("mycar");
var MySuperCar = new SuperCar("my super car");

MyCar.Drive();
MySuperCar.Drive();

First of all, this line

SuperCar = function () { };

was necessary for it to run at all. If I leave it out, I the error “SuperCar is undefined” at this line.

SuperCar.prototype = new Car();

I don’t really understand why declaring SuperCar as an empty function was necessary.

Secondly, when I do run the code I get this result

My name is 'mycar' and my year is '1999'. 
My name is 'undefined' and my year is '1999'. 

Apparently, for MySuperCar, the SuperCar(name) function is never called, but the Car() is.

Adding this line does not help

SuperCar.prototype.constructor = SuperCar;

Neither does this

SuperCar.prototype.constructor = function(name) {
    Car.call(this, name);
};

(I have been running the code inside a script-tag on IE 9 and Chrome 22)

How should I properly define a SuperCar constructor taking a name parameter? Or, put it another way, how can I make the new SuperCar(“my super car”) call behave the way I expected (setting the name to “my super car”)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T03:27:36+00:00Added an answer on June 12, 2026 at 3:27 am

    You should not really create an instance of Car as the prototype, but only create an object that inherits from Car.prototype. For the details, see What is the reason to use the 'new' keyword at Derived.prototype = new Base. Instead, use

    SuperCar.prototype = Object.create(Car.prototype);
    

    Your problem is that your SuperCars are created by the empty function – which returns an object without any properties. Yet, they inherit from new Car(), whose name is undefined. This happened in your fiddle because the function declaration (beginning with the function keyword, check out this explanation) is hoisted (available everywhere in the scope) and overwritten by the line

    SuperCar = function () { };
    

    so that your SuperCar constructor does not call the Car constructor any more. Fixed fiddle

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some time I've been trying to understand regular expressions in JavaScript, but it
I have been trying to understand Javascript equality. Can you please tell me why
I have this javascript function I am trying to understand. I don't know if
I'm trying to understand how to build a JSON object in JavaScript. This JSON
I have been trying to understand how they did those effects in http://artofflightmovie.com/ with
I am trying to understand JavaScript minification and compression processes and have couple of
I am trying to understand unary operators in javascript, and found this reference guide
I'm trying to understand unary operators in javascript, I found this guide here http://wiki.answers.com/Q/What_are_unary_operators_in_javascript
I apologize because this topic comes up a lot, but I have not been
I rather like the prototype way of programming and have been trying to understand

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.