Javascript ES6类组成 [英] Javascript ES6 Classes composition

查看:99
本文介绍了Javascript ES6类组成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力交流被引用的"es6中的同级类",因为按照定义,它们不是真正的父类,这是一种更好的做法或更好的方法.

i'm struggling in what would be a good practice or better approach to communicate 'sibling classes in es6' quoted because they haven't a real parent class, by definition.

让我更好地解释一下:

class Car {
  constructor(typeOfMotor){
    this.motor = typeOfMotor;
    this.mount();
    this.addListener();
  }

  mount() {
     // Some async logic here, and this will return true or false;
  }

  addListener(driver) {
    // Here i want to listen this.mount method and,
    // when return true, then call the ride method in the driver
    // If true:
    driver.ride();
  }
}

class Driver {
  constructor(driverName) {
    this.name = driverName;
  }
  ride(){
    console.log('Highway to hell!');
  }
}

class Race {
  constructor() {
    this.init();
  }

  init() {
    this.car = new Car('v8');
    this.driver = new Driver('michael');
  }
}


var race = new Race;
race.car.addListener(race.driver);

所以基本上,我有一些不需要扩展类的环境,因为我想使它们尽可能地被封装.

So basically, i have some environments where i don't need to extend classes, because i want to keep them as encapsulated as possible.

我有这个顶级类(不是父类,因为其他人没有继承任何东西).

And i have this top Class (not parent because the others are not inheriting anything, though).

问题很简单,在元素之间建立这种交流的最佳方法是什么.

And the question is simple, what would be the best way to create this communication between the elements.

推荐答案

您可以将Driver class实例传递给Car constructor并在该实例内调用任何方法.

You can pass the Driver class instance to the Car constructor and invoke any method within this instance.

我将在这里重新考虑结构和业务逻辑,并检查每个组件应承担什么样的责任.
例如,我认为应该由驾驶员决定何时,但是汽车当然应该在准备就绪时发出信号.
因此,汽车不应调用driver.ride,而应仅向驾驶员发出信号,我正在上车并准备出发,驾驶员应调用驾驶功能.
但这当然是有争议的.

I would rethink the structure and business logic here and check what kind of responsibility each component should handle.
For example, i think it's up to the driver to decide when to drive but of course the car should signal when it is ready.
So the car shouldn't invoke driver.ride and instead just signal the driver i'm on and ready to go, and the driver should invoke the driving function.
But that's arguable of course.

这是您的代码的运行示例(经过一些修改):

Here is a running example of your code (a bit modified):

class Car {
  constructor(typeOfMotor, driver) {
    this.motor = typeOfMotor;
    this.mounted = this.mount();
    this.driver = driver;
  }

  mount = () => {
    console.log('fetching data...');
    setTimeout(() => {
      this.drive()
    }, 1500)
  }

  drive = () => {
    // Here i want to listen this.mount method and,
    // when return true, then call the ride method in the driver
    // If true:
    this.driver.ride();
  }
}

class Driver {
  constructor(driverName) {
    this.name = driverName;
  }
  ride = () => {
    console.log('Highway to hell!');
  }
}

class Race {
  constructor() {
    this.init();
  }

  init = () => {
    this.driver = new Driver('michael');
    this.car = new Car('v8', this.driver);
  }
}


var race = new Race();

这篇关于Javascript ES6类组成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆