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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:18:40+00:00 2026-05-31T05:18:40+00:00

I have an object: function Shape(color, position, coordinates){ this.color = color; this.position = position;

  • 0

I have an object:

function Shape(color, position, coordinates){
    this.color = color;
    this.position = position;
    this.coordinates = coordinates;
    this.shrink = function(){
        reduce(coordinates);
    };
};

And one of many objects with a ‘is a’ relationship to the previous object.

function Sphere(color, position, coordinates, radius){
    this.radius = radius;
    this.role = function(){
        move(coordinates);
    };
};

And a separate Draw function which draws the shapes

function draw(shape){
    moveTo(shape.position);
    setColor(shape.color);
    sketch(shape.coordinates);
}; 

I know I should try use composition where possible but in some cases such as the one above, inheritance is a much more fitting model.

Without frameworks and as simply as possible how could I use prototypal inheritance or any other form to inherit the functionality and attributes of shape so I don’t have to redefine them for every shape type I create and be able to pass arguments into the constructor of the inheriters object so they are passed to the object being inherited from.

EDIT

I searched the posts with similar titles and non of them provided examples regarding my specific situation, passing arguments in constructor and inheriting variables and functions. Also I find answer regarding this subject tend to present multiple options and I was looking for a more definitive answer on the standard approach (by way of example)

With regard to the so called definitive answer Performing inheritance in JavaScript I find that if I was to define all my attributes as described in prototype functions my script would be a mess, also it doesn’t present what to do with arguments passed to the constructor.

  • 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-31T05:18:41+00:00Added an answer on May 31, 2026 at 5:18 am

    Javascript does not have any native construct to achieve this. However, there are utility libraries that help you achieve this.

    If you’re interested in doing this, take a look at Dean Edward’s Base library:

    http://dean.edwards.name/weblog/2006/03/base/

    You can probably use this as is to achieve what you want. At any rate, I can recommend every javascript prorammer to to read that code and try to understand how it works – it will make you a better programmer.

    A very simple solution that does not rely on inheritance at all and also solves the problem of passing in the arguments would be to use object literals as arguments. To assign you simply copy all of the fields from the object literal like so:

    //copy instance variables from source to target 
    function copyInstanceVars(source, target){
        var p,v;
        for (p in source){
            if (source.hasOwnProperty(p)) {
                v = source[p];
                target[p] = v;
            }
        }
        return target;
    }
    
    //Shape, base class.
    var Shape;
    (Shape = function(config) {
        copyInstanceVars(config, this);  //copy instance variables from the config
    }).prototype = {          //class members go in the prototype
        shrink: function(){
            reduce(coordinates);
        }
    };
    
    //Sphere, subclass of Shape
    var Sphere;
    (Sphere = function(config){
        Shape.apply(this, arguments);
    }).prototype = copyInstanceVars(Shape.prototype, {  //inherit methods from Shape
        role: function(){
            move(coordinates);
        };
    });
    

    Then, when instantiating the objects, you’d do:

    var shape = new Shape({
        color: "blue",
        ...,
        coordinates: {x:10, y:20}
    });
    
    var sphere = new Sphere({
        color: "blue",
        ...,
        ...,
        radius: 10
    });
    

    So, in this case, the instance variables are simply those fields present in whatever you pass into the constructor, and the copyInstanceVars function takes care of this copy process. (the hasOwnProperty check in there ensures we’re only grabbing instance variables).

    This example also illustrates how to inherit methods by using that same copyInstanceVars function. But this time we apply it to the prototype of the constructor (because we “declared” the methods as instance variables on the prototype)

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

Sidebar

Related Questions

I have this Class/Object: function Shape(){ this.xArray = new Array(); this.yArray = new Array();
I have the below prog Object.prototype.inherit = function(baseConstructor) { this.prototype = (baseConstructor.prototype); this.prototype.constructor =
If I have a function that returns an object, but this return value is
I have a simple code fragment in JS working with prototype inheritance. function object(o)
I have noticed in Java that you can have a function with object... as
I have an on object like so var obj = {}; function set() {
I have a function that loads a user object from a web service asynchronously.
I have a recursion function that parses an object/array with a global variable. If
I have a label function like : private function formatDate (item:Object, column:DataGridColumn):String { var
I have a perl function that takes an open registry key object. Now, 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.