将 ES6 对象的方法从一个对象动态附加到另一个对象

Dynamically attaching ES6 Object's methods from One object to another

我有这个基本组件 class,看起来像这样

class Component {
    constructor() {
        this.manager = null;
    }
    behavior(){
        //some behavior
    }
}

我希望 GameObject 的实例通过执行类似

的操作来动态继承该行为
var myGameObject = new GameObject();
myGameObject.attach(myComponent);

这样我就可以轻松做到

myGameObject.behavior();

这可以通过 ES6 实现吗?如果没有,我还有什么其他选择?

我确实找到了一种方法来做到这一点,但不确定这是否只是不好的做法

attach(component){
   Object.assign(this,component);
   while (component = Reflect.getPrototypeOf(component)) {
     if(component == Object.prototype) break; // no need to redefine Object
     let keys = Reflect.ownKeys(component)
     for(var i=1;i<keys.length;i++){
       Reflect.getPrototypeOf(this)[keys[i]] = component[keys[i]];
     }
   }
   return this;
}