2

I'd like to upcast an object of a subclass so that it's properties are exclusively that of its superclass, but I've had no luck finding any ways of going about this in JS, and a surprising lack of documentation on weather or not this is even possible, which makes me think more and more it's not, at least in a nice concise way.

The only question I could find on doing this in JS went unanswered and can be found here: How to upcast to limit object properties

Here is a minimal example of what I would like:

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

class B extends A{
    constructor(a, b){
        super(a);
        this.b = b;
    }
}

let a = new A(3); //{a: 3}
let b = new B(4, 5); //{a: 4, b: 5}

//let upcastB = upcast b to type A  //upcastB = {a: 4}

In conclusion, I'd like to know if there is just some standard way to go about upcasting in JS or if the only way to do this is just create two objects of type A and B and remove the different keys from object b.

James Oswald
  • 510
  • 3
  • 12
  • 25
  • 3
    No. First, a class is not a type so you couldn't cast to it even if JavaScript had a notion of casting which it doesn't. Second, in languages that do support casting down casting is rarely done explicitly because a subclass is an instance of its super type and making a cast redundant and it would not change the content or identity of the object. The outlier would be C++ which would slice objects when performing conversions between values instead of references and this behavior is not exactly desirable. – Aluan Haddad Sep 18 '20 at 04:55
  • 2
    What if `B` overrides a method of `A`? You mention one solution being to just remove the `B` keys in order to upcast an instance to `A`, implying that upcasting to `A` would leave the `B`-ish `A` methods as-is. Is that intended? – GirkovArpa Sep 18 '20 at 05:04
  • @GirkovArpa I hadn't even considered that case while writing that as a possible solution. No, ideally that would not be intended. – James Oswald Sep 18 '20 at 05:08
  • 2
    JavaScript is a dynamic language. There's no class declaration that specifies which properties belong to each class. So there's no way to know what to remove when upcasting. – Barmar Sep 18 '20 at 05:19
  • @barmar Thats only partially true, in reality the implementation of the engines actually _do_ track this (it creates shadow classes based on the order of the added properties and methods). But yeah, as a language thats not really a thing. – somethinghere Sep 18 '20 at 05:21
  • 2
    What is the use case for this? What problem is it solving? Even in, say, Java I don't think that's how upcasting works. `B b = new B()` and `A a = (A) b` should leave `b` as-is but just make the compiler treat it as `A`. Functionality, shape, and data of the instance should be preserved. At least to the best of my knowledge. – VLAZ Sep 18 '20 at 05:32
  • 3
    @somethinghere Those are optimizations, there's nothing in the language that explicitly associates properties with a specific class. – Barmar Sep 18 '20 at 05:34
  • To expand on what @VLAZ said, you can simply ignore the extra properties. You can change the prototype of an object to `A.prototype`, and then only the `A` methods will be available, and they won't try to use B's properties. – Barmar Sep 18 '20 at 05:36
  • @VLAZ as Aluan mentioned, I desired more of the C++ style upcasts that preform object slicing. The use case is to have my superclass be a safer version of of a subclass that contains members safe to pass client side. Obviously i could just create a new object has only the safe properties. I just wanted to know if I could use upcasting to slice those properties off, which if these comments are right, No I cant. – James Oswald Sep 18 '20 at 05:38
  • 2
    "*The use case is to have my superclass be a safer version of of a subclass that contains members safe to pass client side.*" sounds like [an XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) in that case. – VLAZ Sep 18 '20 at 05:49
  • you're the one who asked for the use case X, which I already have a solution to. I just wanted to know more about Y and provide a resource for other people since nothing comes up when searching "Upcasting in JS". – James Oswald Sep 18 '20 at 07:23

0 Answers0