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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:30:40+00:00 2026-05-22T21:30:40+00:00

I have the following class hierarchy: Storage Collection EixoCollection OfertaCollection And I have the

  • 0

I have the following class hierarchy:

  • Storage
    • Collection
      • EixoCollection
      • OfertaCollection

And I have the following code:

var ofertaCollection = new OfertaCollection();
var eixoCollection = new EixoCollection();

ofertaCollection.set('mykey', 'myvalue');
alert(eixoCollection.get('mykey')); // it returns 'myvalue', should return nothing

The problem is that ofertaCollection and eixoCollection have properties referencing each other.

Follow the classes:

/**
 * Storage
 * 
 * @returns {Storage}
 */
function Storage(){

    this.storage = []; // itens that have a key

    // sets key
    this.set = function(key, value){
        this.storage[key] = value;
    }

    // gets key
    this.get = function(key){
        return this.storage[key];
    }
}

/**
 * Collection
 * 
 * @returns {Collection}
 */
function Collection(){

}
Collection.prototype = new Storage();

/**
 * EixoCollection
 * 
 * @returns {EixoCollection}
 */
function EixoCollection(){
}
EixoCollection.prototype = new Collection();

/**
 * OfertaCollection
 * 
 * @returns {OfertaCollection}
 */
function OfertaCollection(){
}
OfertaCollection.prototype = new Collection();

What is the problem?

  • 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-22T21:30:42+00:00Added an answer on May 22, 2026 at 9:30 pm

    Firstly, I suggest two changes in your code (that the examples will use): this.storage should be an object {}, since it looks like you never use it as an array. Also, get and set should be in the prototype, otherwise a new instance of those methods will be created for every instance object (unless a smart compiler optimises them, but we won’t assume as much).

    Solution 1: you could perform quasi-inheritance, whereby only your storage methods are given to inheriting classes, but not the storage object:

    function Storage(){}
    storage.prototype = {
        get: function(key){
            return this.storage[key];
        },
        set: function(key, value){
            this.storage[key] = value;
        }
    };
    
    function Collection(){
        this.storage = {};
    }
    Collection.prototype = Storage.prototype; // quasi-inheritance
    
    function EixoCollection(){}
    EixoCollection.prototype = new Collection();
    
    function OfertaCollection(){}
    OfertaCollection.prototype = new Collection();
    
    var ofertaCollection = new OfertaCollection();
    var eixoCollection = new EixoCollection();
    
    ofertaCollection.set('mykey', 'myvalue');
    alert(eixoCollection.get('mykey')); // undefined
    

    Solution 2: Real inheritance, but instantiate a new store for every collection, so during the prototype lookup the function finds the local store. This is identical to the above, but the inheritance would be as before. So I’ll just replace the line that’s different:

    Collection.prototype = new Storage(); // real inheritance
    

    The problem with both of these implementations is that they require the inheriting class to do two things, both inherit the methods, and instantiate a store. Not pretty.

    The easy alternative, and maybe the most intuitive one is to make each use of Storage a composite object, and not an inherited one. A collection has an internal storage, and some extra functionalities, so it fulfils the has-a mnemonic making composition a valid candidate.

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

Sidebar

Related Questions

I have following code class User attr_accessor :name end u = User.new u.name =
I have the following class hierarchy. class Header { IEnumerable<Item> Item { get; set;
Let's say I have the following class hierarchy: TaskViewer inherits from ListViewer<Task> which in
Let's say I have the following class hierarchy in C++: class Base; class Derived1
I have an hierarchy of classes that looks like the following: class Critical {
I have the following class hierarchy: public class RealPeople { } public class Users
We have the following class hierarchy: public interface IManager { object GetObject(int); } public
Suppose I have the following class hierarchy: Class A {...} Class B : A
Let's say I have the following class hierarchy: class Base { protected: virtual void
In my program I have the following class hierarchy: class Base // Base is

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.