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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:18:26+00:00 2026-05-27T23:18:26+00:00

I have a set of classes I derive from a base class as following

  • 0

I have a set of classes I derive from a base class as following (for example):

function Base(){}
Base.prototype.foo = function() { alert("base"); };

function Derived() {}

Derived.prototype = Object.create( Base.prototype );
Derived.prototype.foo = function() { alert("overridden"); };  

var b = new Base();
var d = new Derived();

b.foo();
d.foo();

I would like to add variables to the base class (not Java static-like variables, but those with separate values per class instance). Yet, I would like the Derived class to be able to access them too.

I am no Javascript specialist, I have been playing around a little, but I can’t get it to work. Is this possible to achieve this? Or should I implement setter and getters?

  • 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-27T23:18:26+00:00Added an answer on May 27, 2026 at 11:18 pm

    An example:

    function Base( name ){
        this.name = name;
    }
    
    
    function Derived( name, type ) {
        Base.call( this, name );
        this.type = type;
    }
    Derived.prototype = Object.create( Base.prototype );
    
    
    var b = new Base( 'Peter' );
    var d = new Derived( 'John', 2 );
    

    So, the Base class defines one instance member – 'name'. The Derived class defines one additional instance member – 'type'.

    The key part is invoking the Base constructor inside the Derived constructor invocation – so, internally, you “set up” the instance as if it were a Base instance (you pass in the required arguments), and then you augment the instance with the additional stuff defined only for Derived instances.

    So, in my example above, b has one own property – 'name', whereas d has two own properties – 'name', and 'type'.

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

Sidebar

Related Questions

I have a abstract base class A and a set of 10 derived classes.
I have a set of classes which are all derived from a common base
I have an Object base class, and I have several derived classes called Item,
I have an abstract base class from which many classes are derived. I want
I'm designing/building a system of classes that all derive from a single base class.
I have a base class and several subclasses derived from that base class. I
I have a Base Class, called primitive Graphics. derived from this class are several
I have 3 following classes: public class BaseProperty1{ public string Property1 {get; set;} }
I have a base class called Component which has a number of classes derived
I have a set of XSDs from which I generate data access classes, stored

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.