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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:38:52+00:00 2026-05-19T01:38:52+00:00

Hi I’m new in Javascript OO and want to know more about about inheritance.

  • 0

Hi I’m new in Javascript OO and want to know more about about inheritance. Hope you can provide some advice!

I see this great post:
How to "properly" create a custom object in JavaScript?

which talks about how a class is inherited as I see in other websites, ex.:

function man(x) {
    this.x = x;
    this.y = 2;
}
man.prototype.name = "man";
man.prototype.two = function() {
    this.y = "two";
}
function shawn() {
    man.apply(this, arguments);
};
shawn.prototype = new man;

The above post claims that in order not to call “man”‘s constructor while inheriting, one can use a helper like this instead:

function subclassOf(base) {
    _subclassOf.prototype= base.prototype;
    return new _subclassOf();
}
function _subclassOf() {};
shawn.prototype = subclassOf(man);

While I understand its intention, I don’t see why we can’t call

shawn.prototype = man.prototype;

I see it works exactly the same. Or is there something I’m missing? Thanks in advance!

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

    Well, examples are better than words in my humble opinion. All below examples are using your code, with some additions.

    The first example will prove that using shawn.prototype = new man; you’re calling the constructor twice

    function man(h, w) {
        SendMessage("man is created with height " + h + " and weight " + w);
        this.height = h;
        this.weight = w;
    }
    man.prototype.name = "man";
    man.prototype.double = function() {
        this.height *= 2;
        this.weigth *= 2;
    }
    function shawn() {
        man.apply(this, arguments);
    };
    
    function SendMessage(msg) {
        document.getElementById("Console").innerHTML += msg + "<br />";
    }
    
    window.onload = function() {
        shawn.prototype = new man;
        
        var p = new shawn(180, 90);
        SendMessage("Shawn height: " + p.height);
    }
    <div id="Console"></div>

    As you see, the constructor is called twice – once with no arguments then with the actual arguments you give it.

    The second example just proves that using the subclassOf solve that “double calling” issue.

    function man(h, w) {
        SendMessage("man is created with height " + h + " and weight " + w);
        this.height = h;
        this.weight = w;
    }
    man.prototype.name = "man";
    man.prototype.double = function() {
        this.height *= 2;
        this.weigth *= 2;
    }
    function shawn() {
        man.apply(this, arguments);
    };
    
    function subclassOf(base) {
        _subclassOf.prototype= base.prototype;
        return new _subclassOf();
    }
    
    function _subclassOf() {};
    
    function SendMessage(msg) {
        document.getElementById("Console").innerHTML += msg + "<br />";
    }
    
    window.onload = function() {
        shawn.prototype = subclassOf(man);
        
        var p = new shawn(180, 90);
        SendMessage("Shawn height: " + p.height);
    }
    <div id="Console"></div>

    The third example shows what’s wrong with your idea of shawn.prototype = man.prototype and I’ll explain. shawn inherits from man so I’ve added new method that should affect only shawn, called marriage (that of course cause him to gain some weight ;)) – that method should not affect the base class man as it’s not inheriting from shawn, inheritance is one way only. But…. as you see in the example, ordinary man can also get married – big problem.

    function man(h, w) {
        SendMessage("man is created with height " + h + " and weight " + w);
        this.height = h;
        this.weight = w;
    }
    man.prototype.name = "man";
    man.prototype.double = function() {
        this.height *= 2;
        this.weight *= 2;
    }
    function shawn() {
        man.apply(this, arguments);
    };
    
    function SendMessage(msg) {
        document.getElementById("Console").innerHTML += msg + "<br />";
    }
    
    window.onload = function() {
        shawn.prototype = man.prototype;
        
        var p = new shawn(180, 90);
        SendMessage("Shawn height: " + p.height);
        p.double();
        SendMessage("Shawn height: " + p.height);
        
        shawn.prototype.marriage = function() {
           SendMessage("Shawn is getting married, current weight: " + this.weight);
           this.weight += 20;
        };
        
        p.marriage();
        SendMessage("Shawn weight: " + p.weight);
        
        var q = new man(170, 60);
        q.marriage();
        SendMessage("q weight: " + q.weight);
    }
    <div id="Console"></div>

    Finally, the fourth example shows that using the subclassOf everything work fine, as shawn inherits man properly, and marriage is not passed to the base class.

    function man(h, w) {
        SendMessage("man is created with height " + h + " and weight " + w);
        this.height = h;
        this.weight = w;
    }
    man.prototype.name = "man";
    man.prototype.double = function() {
        this.height *= 2;
        this.weight *= 2;
    }
    function shawn() {
        man.apply(this, arguments);
    };
    
    function subclassOf(base) {
        _subclassOf.prototype= base.prototype;
        return new _subclassOf();
    }
    function _subclassOf() {};
    
    function SendMessage(msg) {
        document.getElementById("Console").innerHTML += msg + "<br />";
    }
    
    window.onload = function() {
        shawn.prototype = subclassOf(man);
        
        var p = new shawn(180, 90);
        SendMessage("Shawn height: " + p.height);
        p.double();
        SendMessage("Shawn height: " + p.height);
        
        shawn.prototype.marriage = function() {
           SendMessage("Shawn is getting married, current weight: " + this.weight);
           this.weight += 20;
        };
        
        p.marriage();
        SendMessage("Shawn weight: " + p.weight);
        
        var q = new man(170, 60);
        if (q.marriage)
            q.marriage();
        else
            SendMessage("marriage is undefined for man");
        SendMessage("q weight: " + q.weight);
    }
    <div id="Console"></div>

    Hope this makes some sense! 🙂

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

Sidebar

Related Questions

No related questions found

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.