Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
In JS can you inherit methods from your class and properties from an instance of your parent class?

In building an order tracking software, I want an inheritance structure that looks something like this

      Default SKU Propreties
              |
        ----------...
        |
    SKU 001
        |
   --------...
   |
Item AAA

I want the Item objects to inherit properties and methods from their parent SKU objects, but also have methods specific to Item objects.

I have tried something like Class Item extends SKU, but of course this just makes the SKU class the Item instance's ancestor, and not the SKU instance which has the relevant properties. I know you can do something like itemInstance = Object.create(skuInstance) to inherit from a SKU instance object, but then itemInstance does not get any methods defined on the Item class.

Thank you for your help :)

Edit, additional info:

I want a SKU instance to have some properties and methods like:

SKU 001
Name: 'Custom T-Shirt'
getLaborCost()

I want each Item instance to inherit the values that are common to all Items under that SKU. I want it also to have properties like proofs that can be unique to each Item instance.

Right now I am achieving this by having "item" object instances have SKU instances as their prototypes. The prototype ancestry looks like this:

SKU Class (with methods)
v
SKU instance (with properties)
v
"item" instance (with properties)

However, this means I can't have items be actual instances of an Item class and get unique methods like attachProof(), which I would not want to be available on the SKU instance object.

from my comment to MikeM: "I want the user during program execution to be able to change the skuInstance.name and all of its items have their itemInstance.name resolve to the current value. I also want the user to be able to override some item instance properties which would otherwise be inherited."

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Here is an example of how you may be able to meet your requirements without using inheritance.

Note that by properties I mean methods also, as methods are properties whose value is a function.

See MDN for some cautions related to the use of Object.assign.

class Item {
  #sku;
  constructor(props) {
    const { sku, ...others } = props; 
    Object.assign(this, others);
    this.#sku = sku;
  }
  get name() { return this.#sku?.name }
  set name(val) { /* disallow */ }
  // Add further properties of all Item instances...
}

class SKU {
  #itemProps;
  constructor(props) { 
    const { itemProps, ...others } = props; 
    Object.assign(this, others);
    this.#itemProps = itemProps;
  }  
  item(props) {
    return new Item({ ...this.#itemProps, ...props, sku: this });
  }
  // Add further properties of all SKU instances...
}

let sku1 = new SKU({ name: 'Custom T-Shirt', itemProps: { color: 'red' } });
let item1 = sku1.item({ size: 'L' });

console.log(item1.name);      // "Custom T-Shirt"
sku1.name = 'Slogan T-Shirt';
console.log(item1.name);      // "Slogan T-Shirt"
console.log(item1.color);     // "red"
console.log(item1.size);      // "L"

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!