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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:29:35+00:00 2026-05-25T16:29:35+00:00

I have a hierarchy of classes, and I’d like that each object had an

  • 0

I have a hierarchy of classes, and I’d like that each object had an ID of the form Classname-integer (examples: Car-0, Car-1, Motorcycle-0, Truck-0, Truck-1, …)

The class hierarchy is

Vehicle
   Car
   Motorcycle
   Truck

The problem is: I want to write just once the code that manages IDs and I’m lost in costructors, prototypes, late binding, and so on.

Example of what I’d like to obtain in pseudocode:

car = new Car
anotherCar = new Car
car.id                  // "Car-0"
anotherCar.id           // "Car-1"
truck = new Truck
truck.id                // "Truck-0"

the Car constructor initializes the object id with the current Car available id, and then increments it so that the next new Car will have a different id. This must not affect other classes IDs.

Ideally, I’d like to write the code just in the base class Vehicle, but I don’t know if it is possible.

My current target language is Coffeescript / Javascript but other languages are welcome along with some reasoning on how it works.

How would you solve this?

  • 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-25T16:29:36+00:00Added an answer on May 25, 2026 at 4:29 pm

    You can use the Vehicle constructor to add a prototype function that will update the ID, like this: http://jsfiddle.net/ZDUEk/.

    (function() {
        var ids = {}; // remember last id
    
        function Vehicle() {
    
        }
    
        Vehicle.prototype.applyVars = function() {
            var name = this.constructor.name, // Car or Truck
                id   = ids[name];
    
            if(!id) {
                id = ids[name] = 0; // init id
            }
    
            this.id = name + "-" + id; // set to form Type-X
    
            ids[name]++; // increment for next id
        };
    
        function Car() {
            this.applyVars(); // set id
        }
    
        function Truck() {
            this.applyVars(); // set id
        }
    
        Car.prototype = new Vehicle;
        Car.prototype.constructor = Car;
    
        Truck.prototype = new Vehicle;
        Truck.prototype.constructor = Truck;
    
        window.Vehicle = Vehicle;
        window.Car = Car;
        window.Truck = Truck;
    })();
    
    var car1   = new Car,
        car2   = new Car,
        truck1 = new Truck;
    
    alert([car1.id, car2.id, truck1.id]);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an hierarchy of classes that looks like the following: class Critical {
I have a small hierarchy of classes that all implement a common interface. Each
I have a hierarchy of classes mapped into hibernate that looks kind of like
I have an hierarchy of classes that I fetch from database by LINQ to
I have a hierarchy of classes that are serialised to XML using XMLSerialiser .
I have a hierarchy of templated classes that are constructed in one place and
I have a quite complex class hierarchy in which the classes are cross-like depending
I have hierarchy of classes like follows (in fact I have more than 3
I have a hierarchy of classes that all derive from a base type and
Assuming that all my classes in the hierarchy have only the default constructor,what are

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.