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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:27:07+00:00 2026-06-11T15:27:07+00:00

jsFiddle Demo I have F which inherits from Shape. F.prototype = Shape.prototype; F creates

  • 0

jsFiddle Demo

I have F which inherits from Shape.

F.prototype = Shape.prototype;

F creates new method with name test.

F.prototype.test = function(){return 'test';};

I understand that if I write F.prototype = Shape.prototype; all the methods which I create in F will be available by other class which inheritance from Shape.

What have I done wrong?

Why do I get errors when I execute the code alert(B.test());?

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

var F = function(){};
F.prototype = Shape.prototype;
F.prototype.test = function(){return 'test';};


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

Triangle.prototype = new F();
Triangle.prototype.constructor = Triangle;
Triangle.prototype.name = 'Triangle';

var my = new Triangle(5, 10);
alert(my.toString());

var Second_class = function(){};
Second_class.prototype = Shape.prototype;
B.prototype = new Second_class();
alert(B.test());

in this example when F which inherits from Shape and Triangle created from F
jsFiddle demo woek well

  • 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-11T15:27:08+00:00Added an answer on June 11, 2026 at 3:27 pm

    I have F which inheritance from Shape.

    F.prototype = Shape.prototype;

    Strictly speaking, you’re not. You are overwriting the prototype property of the F function. To build a real inheritance chain, you would need to use

    F.prototype = Object.create(Shape.prototype);
    

    so that Shape’s properties are still available on F instances, but not the other way round (as both prototypes were the same object). So to answer your first question: Yes, you did.

    See also Why wouldn't I use Child.prototype = Parent.Prototype rather than Child.prototype = new Parent(); for Javascript inheritance?

    why when I execution the code alert(B.test()); it is not working ?

    That’s simple to explain. B (which you forgot to declare in your example, but let’s assume it’s a function) is a Function object. You don’t have any properties assigned to it.
    What you have done is setting it’s prototype property (using the new keyword also sets up the prototype chain correctly, but accidentally creates an instance – Object.create is preferred). So now, any instances of B will inherit from that prototype object:

    var b = new B();
    b.test();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method toString() which is not being inherited from Shape . Why?
I have a script which uses ajax(json) to fetch Google suggestions(xml), demo: JsFiddle I
I have this script which allows to display Bing search results: JsFiddle Demo The
I have a code: Demo jsFiddle which loop three times, each time creating a
Currently I have a stacked bar chart like this ( jsfiddle demo ) I
http://demo.mobiscroll.com/ Mobiscroll is a new date and time picker which dramatically increases usability on
jsFiddle demo link Hi Guys, I want to bind the keypress event handler when
Problem ( jsFiddle demo of the problem ) I'm having some trouble with the
Quick and silly jQuery selector question. Why this does not work : jsFiddle demo
Im having a big headcache with a header design: Here the demo: http://jsfiddle.net/uGECm/ The

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.