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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:30:10+00:00 2026-06-14T07:30:10+00:00

I’m currently in the process of converting a quite large actionscript library to work

  • 0

I’m currently in the process of converting a quite large actionscript library to work in a nodejs project of mine. While doing so I stumbled upon something that could be an issue: Building classes from classes.

Is there a way to use an object as the base for another object(IE: inherits all members from the base object, then overwrites same name members from the extending object)?

Right now this is what I’m doing, though it’s getting a bit difficult to manage now that there are 3+ classes built one on top of another:

// The base object which others may extend
function A() {
    this.a = "pie";
}
A.prototype.yum = function() {
    return this.a + " is AWESOME!";
}

// The "extends A" object.
// Instead of creating an instance of "B", I current just create an instance of "A",
// then adding the members from "B" to it at which point I return the "A" instance.
function B() {
    var a = new A();
    a.b = "pie";
    // Notice how I have to declare the overwriting function here instead of being able 
    // to drop it into B's prototype. The reason this bothers me is instead of just
    // having one copy of the function(s) stored, each time a "new B" is created the
    // function is duplicated... for 100s of "B" objects created, that seems like poor
    // memory management
    a.yum = function () {
        return "I like " + this.a + " and " + this.b;
    };
    return a;
}
console.log((B()).yum());

Is it possible to do something along the following?
I know this isn’t valid, but it gives the idea.

function A(){
    this.a = "pie"
}
A.prototype.yum = function () {
    return this.a + " is AWESOME!";
}
function B(){
    // Throws an "illegal left hand assignment" Exception due to overwriting `this`;
    this = new A();
    this.b = "cake"
}
B.prototype.yum = function () {
    return "I like "+this.a+" and "+this.b;
}
console.log((new B()).yum());

Notes:
1: I know javascript doesn’t have classes; it uses objects and prototypes. Otherwise I wouldn’t be asking.
2: This isn’t the actual code im (trying) to convert; it’s a generalized example
3: Please do not suggest a library. I know at times they are valuable, but I’d rather not have to maintain, depend on and include an entire library for the project.

ANSWER:
I know it’s bad form to alter native member prototypes, but I think this merits it, due to the lack of possible functionality, and the size of it.

Object.prototype.extendsUpon = function (p) {
    var h = Object.prototype.hasOwnProperty;
    for(var k in p)if(h.call(p,k))this[k]=p[k];

    function c(c){this.constructor=c;}
    c.prototype = p.prototype;
    this.prototype = new c(this);
    this.__base__ = p.prototype;
}


function object_Constructor_built_ontop_of_another_constructor() {
    this.extendsUpon(base_Object_to_built_atop_off);
    this.__base__.constructor.apply(this, arguments);

    // From here proceed as usual

    /* To access members from the base object that have been over written,
    * use "this.__base__.MEMBER.apply(this, arguments)" */
}
  • 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-06-14T07:30:11+00:00Added an answer on June 14, 2026 at 7:30 am

    Very much possible. You can do it in multiple ways, the more complete is used in coffeescript:

        var ClassBase, ClassTop,
          __hasProp = {}.hasOwnProperty,
          __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
    
        ClassBase = (function() {
    
          function ClassBase() {}
    
          return ClassBase;
    
        })();
    
        ClassTop = (function(_super) {
    
          __extends(ClassTop, _super);
    
          function ClassTop() {
            return ClassTop.__super__.constructor.apply(this, arguments);
          }
    
          return ClassTop;
    
        })(ClassBase);
    

    There is going to be some boilerplate code. ClassTop is inheriting everything from ClassBase. The classes don’t have much inside them other then an __extend, a (function(_super... and some constructor boilerplate but it’s fairly simple.

    The inheritance is mostly managed by the __extends boilerplate that does some magic. The full __extends method is beautified here:

        __extends = function (child, parent) {
            for (var key in parent) {
                if (__hasProp.call(parent, key)) child[key] = parent[key];
            }
            function ctor() {
                this.constructor = child;
            }
            ctor.prototype = parent.prototype;
            child.prototype = new ctor();
            child.__super__ = parent.prototype;
            return child;
        };
    

    Again, much less scary then before. You’re basically checking properties that the parent has and applying them to the child. More information can be found here: http://www.jimmycuadra.com/posts/coffeescript-classes-under-the-hood

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am currently running into a problem where an element is coming back from
I have thousands of HTML files to process using Groovy/Java and I need to
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.