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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:48:29+00:00 2026-06-16T01:48:29+00:00

I’m trying to write a javascript class which extends from a parent class, overloads

  • 0

I’m trying to write a javascript class which extends from a parent class, overloads one method and changes it slightly.

E.g it checks some variables, if they are set to true, then it executes the same method on parent, otherwise it executes some different code.

This is what I’ve come up with:

function Dad(name)    
{
    this.yell = function()
    {
        console.log( 'GRRRRRRR ' + name);
    }
}

function Kid(name, age)    
{
    var parent = new Dad(name);


    parent.yell = function()
    {
        if (age < 15)
            console.log( 'BAAAAA ' + name );
        else
            parent.yell(); //This is the problem line, how to call this method on
                           //parent object
    }
    return parent;
}

var k = new Kid('bob', 13);
k.yell();

However the issue is, how to call the method on the parent object.

Any ideas?

  • 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-16T01:48:30+00:00Added an answer on June 16, 2026 at 1:48 am

    Use prototypes. They allow you to access the methods of the superclass, but without instantiating it.

    Then, from the child class, you can do SuperClass.prototype.instanceMethod.call(this), which is basically super in most typical OO languages, but JS doesn’t help you figure out what the superclass is. So you have to keep track of it yourself.

    // Superclass
    function Dad() {};
    Dad.prototype.yell = function() {
        console.log("Do your homework!");
    }
    
    // Subclass
    function Kid(age) {
        // calls the constructor of the superclass.
        // in this case, the Dad() constructor does nothing, so it's not required here.
        Dad.call(this);
    
        // save constructor argument into this instance.
        this.age = age;
    };
    
    // Inheritance happens here, prototype is an instance of superclass
    Kid.prototype = new Dad();
    
    // Make sure the constructor is properly assigned.
    Kid.prototype.constructor = Kid;
    
    // Override an instance method.
    Kid.prototype.yell = function() {
        if (this.age < 18) {
            console.log('Waaaaa, I hate homework');
        } else {
            // calls the yell method of the superclass
            Dad.prototype.yell.call(this);
        }
    }
    
    // make a kid.
    var k = new Kid(13);
    k.yell(); // 'Waaaaa, I hate homework' 
    
    // make an old kid.
    var k = new Kid(30);
    k.yell(); // 'Do your homework!' 
    

    OO inheritance in JS can be messy, but there is some things out there to help.

    • Object.create
    • Resig’s Simple javaScript Inheritance
    • Coffeescript classes

    To name a few.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I am trying to render a haml file in a javascript response like so:
I'm trying to select an H1 element which is the second-child in its group
I have a text area in my form which accepts all possible characters from
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am reading a book about Javascript and jQuery and using one of the
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.