0

say I had some class hierarchy like below. Is there a way for the engine class to have access to the parent's someMethod() method?

class vehicle{
  constructor(){
    this.eng = new engine();
    ...
  }

  someMethod(a){
    ...
  }
  ...
}



class engine{
  ...
}
3
  • Not without explicitly passing the vehicle instance to the engine. But why does the engine need to access vehicle methods? Maybe there is a better way to design this. Commented Jan 17, 2019 at 18:51
  • There is no such thing as a "parent object". Commented Jan 17, 2019 at 19:04
  • Your code doesn't show a class hierarchy and thus no parent class. Did you mean for engine to inherit from vehicle? If so, a method in vehicle can just call this.someMethod() to call any method on the object whether defined in this class or in a parent class. Commented Jan 17, 2019 at 19:56

2 Answers 2

1

There are a number of ways to go about what you want, and you have to ask yourself: conceptually, how is this supposed to work?

You have to rethink the entire situation. Bottom line is, why does the engine care about what vehicle it is in? The vehicle only cares about what the engine is doing, not the other way around.

So, say you have a start method on the vehicle. It may look like

start() {
  this.engine.start().then(function(infofromengine) {
    console.log('engine started', infofromengine);
  }, function(errorfromengine) {
    console.log('error in engine start', errorfromengine);
  }
}

and in your engine you'd have that start method that returns a Promise and whatever info the vehicle might need to know about the engine;

you might also, upon initializing the engine in the vehicle, set up all sorts of event emitters, and in your vehicle constuctor hook into the emitters.

Taking this one step further, you might have some child components that are supposed to, perhaps, be aware of eachother. So you could "wire them up" in construction. An example contrived from this might be in your vehicle constructor

constructor() {
  this.checkEngineLight = new Light();
  this.engine = new Engine({checkEngineLight});
}

and in engine,

constructor(config) {
  let {checkEngineLight} = config;
  this.engineLight = checkEngineLight;
}

someEngineAction() {
  if (some_error_happens) this.engineLight.toggle(true, someerrorid);
}

there is a reason we are called "software engineers" or "software architects". we don't just write code, we have to consider how the code interacts with all the moving parts.

But to answer your question more directly, in the case where the "engine" absolutely has to have direct access to the "vehicle" you would have to tell the engine about the vehicle... just like how you told it about the check engine light: new Engine(this) and the engine constructor would be constructor(vehicle)

Sign up to request clarification or add additional context in comments.

Comments

0

There are two solutions to this problem

First, if you inherit the properties of a parent like this:

class engine extends vehicle {
  someMethod(a) { // method name has to be the same name as parent, calling super calls vehicle's someMethod(a) method
    super(a); // vehicle.super called
  }
}

or create an instance of vehicle and call the method

class engine {
  someMethod(a) { // can be any name, as long as its using v.someMethod(a)
    const v = new vehicle();
    v.someMethod(a);
  }
}

6 Comments

from what I understand using extends allows you to call the methods from the inherited class but not on the parent object. If engine extends vehicle and there is an engine object that's a child to a vehicle object, if engine uses super it will call it on itself using a copy of the function, instead of running the parent's method which means if the functions changes object data, the parent would remain unchanged. There's a similar problem with the second option too
Neither solutions makes sense here. An engine is not a vehicle. A vehicle has an engine, not the other way round.
@anthiusbalarawXanthius we can't know for sure what your real objective. We'll just give you what you ask for.
@FelixKling I totally understand what you mean by that, but I have no idea what someMethod is, and why would it ever want to access something the car has (why would an engine do that in real life as well, except for checking the ignition).
Why would the engine check ignition? Ignition should act upon the engine :-)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.