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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:19:02+00:00 2026-05-22T18:19:02+00:00

I have a module Vehicle that contains general vehicle info. I have another module

  • 0

I have a module Vehicle that contains general vehicle info. I have another module Car, which adds more functionality to Vehicle object.

// Pseudo code only. The final functions do not have to resemble this
var vehicle = require('vehicle')
vehicle.terrain = 'Land'
var car = vehicle.createCar()
// car and anotherCar will have unique Car-related values,
// but will use the same Vehicle info
var anotherCar = vehicle.createCar()

I am looking at using Object.create for the Car module, but not sure where the Object.create calls should go.

  • Should I have a constructor in the Car module that takes an instance of a Vehicle object and does an Object.create with the Vehicle instance as the prototype?
  • Or should the Object.create happen in a function on the Vehicle object, like createCar? My issue with this way, is Car should care that it’s derived from Vehicle, Vehicle shouldn’t know Car requires that.
  • Or even if Object.create is the right approach.

Please, any examples and best practices would be greatly appreciated.

Update:

I changed the example to better reflect the inheritance problem I’m trying to solve.

  • 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-22T18:19:02+00:00Added an answer on May 22, 2026 at 6:19 pm

    imo, you’re describing a builder pattern rather than inheritance I think — I wouldn’t use object.create for this. A VehicleBuilder is responsible for constructing an object that has certain properties associated with it.

    var builder = new VehicleBuilder();
    builder.terrain = 'Land';
    builder.wheelCount = 2;
    builder.color = "blue";
    var motorcycle = builder.createVehicle();
    

    It might use something like:

    VehicleBuilder.prototype.createVehicle = function(){
        var me = this;
        return new Vehicle({
             color: me.color,
             terrain: me.terrain,
             wheelCount: me.wheelCount
        });
    }
    

    If you look at the typical inheritance pattern in js, its something much more well defined and uses two primary patterns in node. One is util.inherits. Its code is simple: https://github.com/joyent/node/blob/master/lib/util.js#L423-428

    exports.inherits = function(ctor, superCtor) {
      ctor.super_ = superCtor;
      ctor.prototype = Object.create(superCtor.prototype, {
        constructor: { value: ctor, enumerable: false }
      });
    };
    

    And the second is calling the parent constructor in the child class constructor.

    function ChildClass(){
        SuperClass.call(this); // here
    }
    

    Example: https://github.com/joyent/node/blob/master/lib/stream.js#L25-28

    So instead of a vehicle taking a bunch of properties or another object in its constructor, you use the prototype chain and the constructor to define custom subclass behavior.

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

Sidebar

Related Questions

I have a module which contains a lot of functions (more than 25). I
I have a module called MyApp, and another module called MyAppTests which has a
I have a module which adds little piiece of code to specified static blocks
I have a module that uses ctypes to wrap some functionality from a static
I have module application. When I run it, the main window of that app
I have module that implements custom content type via NodeAPI hooks ( hook_insert ,
I have a module that reads the StandardError of a process. Everything works fine,
I have a module which runs standalone in a JVM (no containers) and communicates
Let's say that I have a module that has a Queue in it. For
i have written module which reads and write on /proc file and is working

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.