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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:44:47+00:00 2026-05-26T10:44:47+00:00

Perhaps I’m doing this wrong. I want to set up a base class and

  • 0

Perhaps I’m doing this wrong. I want to set up a base class and two classes that inherit from that class. However, it is telling me that one child is an instanceof the other child… that can’t be correct, can it? What am i doing wrong here?

function BaseClass(){}
function Child1(){}

Child1.prototype = BaseClass.prototype;
Child1.prototype.constructor = Child1;

function Child2(){}
Child2.prototype = BaseClass.prototype;
Child2.prototype.constructor = Child2;

console.log((new Child1() instanceof Child2)); // true
  • 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-26T10:44:48+00:00Added an answer on May 26, 2026 at 10:44 am

    Child1.prototype = BaseClass.prototype;

    Should be Child1.prototype = Object.create(BaseClass.prototype)

    You don’t want the prototype object to be the same, you want a new prototype object that inherits from BaseClass

    Live Example

    Disclaimer: Object.create is ES5, use the ES5-shim for legacy platform support.

    To give you a more thorough example :

    // Create base prototype
    var Base = {
      method: function () {
        return this.things;
      },
      constructor: function (things) {
        this.things = things;
      }
    };
    // Link constructor and prototype
    Base.constructor.prototype = Base;
    
    // Create child prototype that inherits from Base
    var Child = Object.create(Base);
    // overwrite constructor
    Child.constructor = function (things) {
      things++;
      Base.constructor.call(things);
    }
    // Link constructor and prototype
    Child.constructor.prototype = Child;
    
    // Swap prototype and constructor around to support new
    ChildFactory = Child.constructor;
    
    var c = new ChildFactory(41);
    console.log(c.method()); // 42
    

    Visually:

    var Base = { ... }

    here we simply create an object with methods and properties. We want to be able to create an instance of this object. So Base is a “class” and all it’s methods and properties can be accessed from the instances (including constructor).

    Base.constructor.prototype = Base;

    You need to link the “Constructor function” which is a function you can call with new which will give you a new instance of the object ConstructorFunction.prototype together with the prototype object. This is basically a bit of glue you need to do manually because ES does not do this for you.

    If you forgot to do this then the constructor function (which is X.constructor) doesn’t have a .prototype property to make instances inherit from.

    var Child = Object.create(Base);

    Create a new object whose [[Prototype]] is Base. This basically is the start of your prototype chain

    Child -> ([[Prototype]] = Base) -> ([[Prototype]] = Object.prototype) -> null
    

    Child.x = ...

    Now we add properties to our child object which are the methods instances of Child will inherit. Basically we are creating a new prototype object to inherit from. All instances will share the methods and also share the methods up the prototype chain (including Base).

    ChildFactory = Child.constructor;

    the new key word only works on constructor functions and not prototype objects so we need to basically say “switch our prototype object variable to the constructor function variable”

    Personally when building up “classes” I find the prototype objects really pleasant to handle directly and when creating instances I find the constructor functions pleasant to handle.

    Now when we call var c = new Child(41);

    It will call the constructor function we defined which will increment things, then call the base constructor.

    Note at this point the prototype chain of c looks like

    c -> ([[Prototype]] = Child) 
      -> ([[Prototype]] = Base) 
      -> ([[Prototype]] = Object.prototype) 
      -> null
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

perhaps I'm asking the wrong question. I have code like this: class ExpressionGrammar(Grammar): def
Perhaps this is a trivial question, however it's something that is seriously annoying me.
Perhaps I am not asking or searching for this correctly: I want to have
Perhaps there's a better way to set this up so I'm open to suggestions.
Perhaps I'm going about this all wrong (and please tell me if I am),
perhaps you can help me to solve this problem. I want to display a
Perhaps a noob question, but take this T-SQL query in mind: UPDATE Table SET
Perhaps you can help. I have a main class that creates a MySql connection.
Perhaps I'm going about this the wrong way, but I'm using HXT to read
Perhaps this is the wrong place for this, but I'm not quite sure where

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.