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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:57:30+00:00 2026-05-24T17:57:30+00:00

http://jsbin.com/ifoguf/14/edit#javascript I attempted Isolating the Inheritance Part into a Function, but when I create

  • 0

http://jsbin.com/ifoguf/14/edit#javascript

I attempted Isolating the Inheritance Part into a Function, but when I create the object nothings is alerted, no errors are returned, so I’m stumped how to debug, and for a solution.

function Shape(){}
// augment prototype
Shape.prototype.name = 'shape';
Shape.prototype.toString = function() {return this.name;};

function TwoDShape(){}


// augment prototype
TwoDShape.prototype.name = '2D shape';



function Triangle(side, height) {
this.side = side;
this.height = height;
}


// augment prototype
Triangle.prototype.name = 'Triangle';
Triangle.prototype.getArea = function(){return this.side * this.height / 2;};




var myTriangle = new Triangle(5, 10);

function extend(Child, Parent) {
var F = function(){};
F.prototype = Parent.prototype;
Child.prototype = new F();
Child.prototype.constructor = Child;
Child.uber = Parent.prototype;
}


extend(TwoDShape, Shape);
extend(Triangle, TwoDShape);


var myTriangle = new Triangle(5, 10);


alert(myTriangle.getArea());

alert(myTriangle.toString());

var s = new Shape();
alert(s.name);

TIA

  • 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-24T17:57:31+00:00Added an answer on May 24, 2026 at 5:57 pm

    Your code seemed a bit complex. See a simpler example (remember to switch on a debugging tool to see log messages).

    It’s worth noting that inheritance in JavaScript works by following prototype chains. That means every object holds a reference to a prototype object. This prototype object is just any ordinary object, too. The key is is that whenever a property (or function) is not found within an object, its prototype object is checked. This follows the prototype links until either a value was found or no prototypes are left over.

    The prototype link is stored within an object whenever an object is created and points to the object as specified in the prototype property of the object’s constructor function. You can inspect the prototype in Firefox by checking an object’s __proto__ property.

    In the linked example, there are 3 constructors:

    • Shape
    • Triangle
    • createPrototype (helper to get an empty object with defined prototype link)

    In the linked example, there are 2 prototype objects:

    • Shape.prototype (contains shared properties for shapes)
    • Triangle.prototype (contains shared properties for triangles)

    First a shape is created by calling new Shape(). This creates a new object since new is written in front of the function named Shape. The prototype link of the object (__proto__ in Firefox) points to Shape.prototype because this property of the constructor function gives the prototype link’s target. The link is set on object instantiation.

    As a result whenever you try to access a property of the shape the property is retrieved as follows. First, shape is searched for the property. If it’s there, use it. If not, the prototype object shape.__proto__ is retrieved which refers to the same object as Shape.prototype. Now, the prototype object is searched for the property.

    For the case of triangle the thing is a bit more complex because multiple prototype links are involved. The important thing is that we need to create an empty object to hold the triangles’ shared properties which also needs to have its prototype link set to point to Shape.prototype. This makes shape’s prototype the fallback of triangle’s prototype.
    It is archived by the anonymous function that includes createPrototype. It needs to be included because every invocation of the anonymous function needs to manipulate the prototype property of a constructor function (called createPrototype in our case). After the empty prototype object with the prototype chain is created, it is instantly returned and assigned to Triangle.prototype. After that any object created by new Triangle() has the desired prototype chain.

    Once this is set, we can add all shared properties of triangles into the Triangle.prototype object that was just created. This is shown by overriding paint and by adding getArea. Note this overriding actually works because the paint function for triangles is found earlier than the paint method for shapes and the first found reference/property is used.

    To summarize, all property accesses on objects use prototype chains for fallbacks. In our case the chain for shape is shape→Shape.prototype and the one for triangle is triangle→Triangle.prototype→Shape.prototype.

    You might want to assign a name to the anonymous function (that includes createPrototype) to have a tool for created object hierarchies. This is shown in the updated example and ends up with the following helper:

    function makePrototype(parent){
        // We need a new object whose prototype link points to parent.prototype
        function createPrototype(){
        }
        createPrototype.prototype = parent.prototype;
        return new createPrototype();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

http://jsbin.com/ifoguf/19/edit I'm unable to access the get area function of Triangle, and getting a
http://jsbin.com/idazeg/edit#javascript,html Can someone tell me how and why this is working? $('#pp').click (function ()
Please see my code here http://jsbin.com/ijoxa3/edit var drawHorizondalLine = function(x1,y1,x2,y2, color) { var width
http://jsbin.com/ehuke4/16/edit When Im dragging in Chrome and FF its ok and dragging But on
I'm using the code found in this jsbin: http://jsbin.com/asahe5/10/edit There is a function within
http://jsbin.com/eyikac/3/edit#javascript,html,live - this works for me. I thought I wasn't allowed to do cross-domain
Code as reference: http://jsbin.com/aboca3/2/edit In this example above (thank you SLaks) I am truncating
EDIT: See this in action here: http://jsbin.com/emobi/5 -- and that's using mouseenter/mouseleave. I have
Edit: I put this snippet of code in jsbin: http://jsbin.com/eneru I am trying to
http://jsbin.com/emoba5/3/edit click here to view demo Does anybody know how to make the image

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.