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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:37:03+00:00 2026-06-18T09:37:03+00:00

I just got into JavaScript and I’m kind of puzzled by its object-oriented behavior.

  • 0

I just got into JavaScript and I’m kind of puzzled by its object-oriented behavior.
I was just trying to create a class Point2D with x,y members and extend it with a Point3D class with x,y,z members.
The behavior I’m trying to achieve is something like, let’s say it in C#:

class Point2D
{ 
   int x, y;
   public Point2D(int x, int y) { this.x = x; this.y = y; }
}
class Point3D : Point2D
{
    int z;
    public Point3D(int x, int y, int z) : base(x, y) { this.z = z; }
}

I read a lot of stuff but I don’t seem to really find what I’m looking for.
Here’s what I’ve come up to so far:

function Point2D(x, y) { this.x = x; this.y = y; }
Point2D.prototype.constructor = Point2D;
function Point3D(x, y, z) { Point2D.prototype.constructor.call(this); this.z = z; }
Point3D.prototype = new A(); // see latter explanation
Point3D.prototype.constructor = B;
var p = new Point3D(10, 20, 30);

Which is obviously wrong.

Now, I know I should do something like Point3D.prototype = new A(x, y) but I don’t want to create a prototype with fixed x,y coordinates and variable z.
It must be very simple, but I’m just not getting it, I can’t seem to call the superclass constructor or to make it behave properly.

  • 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-18T09:37:05+00:00Added an answer on June 18, 2026 at 9:37 am

    JavaScript’s prototypal inheritance provides a couple of different flexible ways to perform the kind of polymorphic constructors you’re looking for. In your particular example, you want something like this:

    function Point2D(x, y) {
      this.x = x;
      this.y = y;
    }
    
    function Point3D(x, y, z) {
      Point2D.call(this, x, y);
      this.z = z;
    }
    

    Fussing with explicitly setting the constructor on the prototype is not strictly necessary here. (It is really only necessary when you are creating the prototype “all at once” — e.g., with an object.)

    For a detailed discussion of object-oriented JavaScript, I’d recommend Nicholas Zakas’ Principles of Object-Oriented Programming in JavaScript (ebook) or the discussion of prototypes and inheritance in his other book, Professional JavaScript for Web Developers.

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

Sidebar

Related Questions

I've got into a rather simple, yet annoying problem. I'm trying to create a
I just got into python very recently and now I'm practicing by (what I
I've recently been doing a lot of Objective-C programming, and just got back into
I am running into a problem that just doesn't seem right. I've got a
We just got our first major Perl CGI assignment in my CS class. Our
I've always been dabbling in javascript, but never really got hugely into writing a
Im just in the middle of refactoring my javascript into a modular fashion using
Has anyone got to some good code to zoom into an image using javascript?
Just got the new iPad, and when I touch the microphone and say a
i just got a call from Hosting company, they claimed that my application creates

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.