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 6619533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:58:54+00:00 2026-05-25T20:58:54+00:00

Using pure JavaScript to do inheritance, this is what I usually do: function A()

  • 0

Using pure JavaScript to do inheritance, this is what I usually do:

function A() {}
A.prototype.run = function () {};

function B() {}
B.prototype = new A;
B.prototype.constructor = B;

Since there is no arguments to pass into the constructor, new A has nothing to complain about. Now, I haven’t figured out a good way to do inheritance if the constructor has arguments to pass. For example,

function A(x, y) {}
A.prototype.run = function () {};

function B(x, y) {}
B.prototype = new A;
B.prototype.constructor = B;

I could pass some arbitrary values like:

B.prototype = new A(null, null);

In some cases, I may need to validate x and y in the constructor of A. In some extreme cases, I need throw errors when checking x or y. Then, there is no way for B to inherit from A using new A.

Any suggestions?

Thanks!

  • 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-05-25T20:58:54+00:00Added an answer on May 25, 2026 at 8:58 pm

    Well, if you want to make B.prototype an object that inherits from A.prototype, without executing the A constructor, to avoid all possible side-effects, you could use a dummy constructor to do it, for example:

    function tmp() {}
    tmp.prototype = A.prototype;
    B.prototype = new tmp();
    B.prototype.constructor = B;
    

    You could create a function to encapsulate the logic of the creation of this new object, e.g.:

    function inherit(o) {
      function F() {}; // Dummy constructor
      F.prototype = o; 
      return new F(); 
    }
    
    //...
    B.prototype = inherit(A.prototype);
    B.prototype.constructor = B;
    

    If you target modern browsers, you could use the ECMAScript 5 Object.create method for the same purpose, e.g.:

    B.prototype = Object.create(A.prototype);
    B.prototype.constructor = B;
    //..
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there anyway to achieve this that works cross-browser? (Keeping solutions to pure JavaScript,
I want to make a function either using pure Javascript or also benefiting from
I have a pure C++ application developed using VC 6.0. I would like this
Would it be possible to write a basic SSH client in pure javascript using
How to create a pure JavaScript (without using any library) plugin which looks like:
Basically this, but in pure javascript: How to get 'value' of select tag based
using pure javascript can i select all a tags with href=#xyz. And also i
How can I replace the selected text with another text using PURE javascript, in
Does anyone know of a small, limited functionality lightbox built with pure javascript? This
I am using pure Javascript and JSP for my Front End Purpose. On click

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.