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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:55:37+00:00 2026-05-27T01:55:37+00:00

I am quite experienced with coding in Javascript, but there’s still one thing I

  • 0

I am quite experienced with coding in Javascript, but there’s still one thing I can’t really wrap my head around.

I have a superclass, let’s say Category. Now I want to create some instances of a subclass, let’s say Post, from inside the Category instance. I want Post to have its own properties, but it also needs to be able to access properties of its parent. So this is the concept:

/* Superclass */
function Category(catID) {
    this.catID = catID;
    this.posts = [];

    this.addPost = function(id, content) {
        var post = new Post(id, content);

        post.prototype = Category;

        this.posts.push(post);
    }

    this.getPosts = function() {
        for(post in this.posts){
            this.posts[post].getContent();
        }
    }
}

/* Subclass */
function Post(postID, content) {
    this.postID = postID;
    this.content = content;

    this.getContent = function() {
        console.log('Post: '+ this.postID);
        console.log('Category: ' + this.catID);
        console.log('Content: ' + this.content);
    }
}

var cat1 = new Category(1); 
var cat2 = new Category(2);

cat1.addPost(101, 'First post in first category');
cat1.addPost(102, 'Second post in first category');
cat2.addPost(201, 'First post in second category');
cat2.addPost(202, 'Second post in second category');

cat1.getPosts();
cat2.getPosts();

I got stuck on the line post.prototype = Category. I would expect that now Post inherits the properties of Category, but it doesn’t happen.

Can someone please help me out with this?

  • 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-27T01:55:38+00:00Added an answer on May 27, 2026 at 1:55 am

    JavaScript does not have classes. The prototype of an object is another object. If you change your prototype assignment to this, it should work:

    post.prototype = this;
    

    I don’t think this is what you want to do, however. An inheritance relationship does not make sense in this case. A Post is not really a type of Category. IMO, I would use composition instead of inheritance:

    post.category = this;
    

    That way, from your post, you should be able to access the members of the category through the category member.

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

Sidebar

Related Questions

Quite experienced but self taught with PHP/MySQL and playing about with Rails. Really love
I'm quite experienced in PHP but I don't quite use mod_rewrite (although I should).
I'm a starter with Emacs (but quite experienced Vim user) and trying to play
Pre-warning : There are some other questions similar to this but don't quite answer
I'm quite experienced in XAML/WPF but am new to Flex/Flex4. I'm trying to build
I'm an experienced programmer, but a novice to XSLT and am finding it quite
I've had a javascript running for quite som time and I've never experienced any
I'm rather new to Ant but I have experienced it's quite good pattern to
I'm a pretty experienced Java programmer that's been doing quite a bit of Win32
Quite often in ANSI C code I can see parenthesis sorrounding a single return

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.