0

As the title said, I have two class like below:

class A {
    constructor() {
       this.a = 1
       this.b = 2
    }
}
Class B extends A {
    constructor() {
       super()
       this.c = 3
    }
}

Now, I instance class B

const b1 = new B()

Can I use some method to get the { a:1, b:2 } from b1. Because I need to post some data through API. But my model is something like class B. but the interface of the API is like class A. So I just want to get the parent object from the child object. Thanks in advance.

Veeramani R
  • 145
  • 7
Courage
  • 543
  • 5
  • 25
  • 6
    if B extends A then .... b1.a would be 1, or did I miss something important? – Jaromanda X Sep 21 '22 at 10:47
  • *but the interface of the API is like class A* - an aspect of inheritance in OOP is that every single `B` object is an `A` object too, and can be used where `A` objects are expected. – tevemadar Sep 21 '22 at 10:53
  • 1
    Practical check: try `console.log(b1 instanceof B, b1 instanceof A);`. – tevemadar Sep 21 '22 at 10:55
  • @JaromandaX Yes, you are right. But if the interface of API have a lot of property should be passed. I don't think b1.a b1.b ...... b1.z is a good code. – Courage Sep 21 '22 at 14:00
  • @Courage why is it bad code? This is how JS works, [prototypical inheritance](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain). – evolutionxbox Sep 21 '22 at 14:01
  • @tevemadar I think I can get your point. But if I pass some redundant property to the API. I am afraid that it will cause some confusion for other coder. – Courage Sep 21 '22 at 14:04
  • @evolutionxbox I am not sure if I can use this code new A(b), of course, the constructor should be changed in class A. Maybe I can get the result like b1.a and b1.b – Courage Sep 21 '22 at 14:13
  • Does this answer your question? [Elegant way to code partially copy objects in Javascript?](https://stackoverflow.com/questions/23886484/elegant-way-to-code-partially-copy-objects-in-javascript) – tevemadar Feb 13 '23 at 10:11

1 Answers1

1

Can I use some method to get the { a:1, b:2 } from b1?

I interpret this to mean that you want an object with only properties a and b from b1. If that's your meaning, then you can simply pick those properties from b1 and post the new object:

function pick (obj, keys) {
  const result = {};
  for (const key of keys) result[key] = obj[key];
  return result;
}

class A {
  constructor () {
     this.a = 1;
     this.b = 2;
  }
}

class B extends A {
  constructor () {
     super();
     this.c = 3;
  }
}

const b1 = new B();

// Post this:
const data = pick(b1, ['a', 'b']);

console.log(data); // { a: 1, b: 2 }
jsejcksn
  • 27,667
  • 4
  • 38
  • 62
  • Thanks for your response. But what I want is some decent way. Like downcasting or upcasting in JAVA – Courage Sep 21 '22 at 13:58
  • 2
    @Courage do not expect JavaScript to work like Java, they're not the same. JS doesn't have those concepts. Also have a look at https://stackoverflow.com/questions/44291524/javascript-downcasting-object – evolutionxbox Sep 21 '22 at 14:02