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

  • SEARCH
  • Home
  • 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 8438125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:37:18+00:00 2026-06-10T07:37:18+00:00

I’m not new to JavaScript, but I never could understand a certain thing about

  • 0

I’m not new to JavaScript, but I never could understand a certain thing about its prototypal inheritance.

Say we have Parent and Child “classes” (functions Parent and Child that create objects). To be able to create Children, we first need to

Child.prototype = new Parent();

Here is the difficulty: by assigning the prototype to Child, we get one more object Parent that doesn’t do anything in our code, just shares its properties with Children. But the constructor of Parent is still called! If parent represents, for example, a certain UI object, then in our application we will have one more such object which we actually didn’t wish to create! And that, of course, may and will affect the state of our application.

I see a way to cope with this: to pass certain arguments to Parent constructor indicating that the object we are creating is just for prototype, not for general use, like:

RedButton.prototype = new Button(FOR_PROTO_ONLY);

and then in Parent constructor decide whether to do any displayable stuff or not. But this is such an ugly workaround!

In class-oriented languages, e.g. Java, we don’t have such issue at all because inheriting doesn’t suppose calling any additional functions. What should I do to not use such ugly techniques in my programs and still be able to create a nice-looking prototype hierarchy?

  • 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-10T07:37:20+00:00Added an answer on June 10, 2026 at 7:37 am

    One thing you can do is assign the instance of the parent to it’s prototype property in the constructor. This means that you do not have to create an extraneous instance of the parent and therefore alleviates the issue you mentioned where you could end up with an extra GUI component being defined. However, it does mean that you must have at least one instance of the parent before instantiating an instance of the child and therefore this alone limits its usefulness to very specific circumstances.

    Here’s an example: http://jsfiddle.net/xwwWZ/

    var Child = function() {
    
        this.throwATantrum = function() {
    
            alert("Waaaaaaaaaah");
    
        }
    };
    
    
    var Parent = function() {
    
        // Set the prototype here to avoid having to create an extra instance elsewhere.
        Parent.prototype = this;
    
        this.sayHello = function() {
    
            alert("Hello!");
    
        }
    };
    
    
    // We must, however, instantiate an instance of the parent before we can
    // instantiate an instance of a child.
    var p = new Parent();
    
    
    Child.prototype = Parent.prototype;
    
    var c = new Child();
    
    c.throwATantrum();
    c.sayHello();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I am reading a book about Javascript and jQuery and using one of the
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into
I am trying to render a haml file in a javascript response like so:

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.