简体   繁体   English

如何从子实例访问 class 的实例?

[英]How can I access an instance of a class from a child instance?

I have a class named "Main", with one instance named "main".我有一个名为“Main”的 class,其中一个实例名为“main”。 The "main" instance has two properties that are instances of other classes, named "player" and "background". “main”实例有两个属性,它们是其他类的实例,名为“player”和“background”。 I want the child instances to be able to use each other, as well as the "main" instance.我希望子实例能够相互使用,以及“主”实例。 Below is my attempt, which throws an error: "main is undefined".下面是我的尝试,它引发了一个错误:“main is undefined”。

class Player {
    constructor() {
    }

    x = 10;
    y = 20;
};

class Background {
    constructor() {
    }

    backgroundCenterX = main.player.x;
    backgroundCenterY = main.player.y;
    // the previous two lines throw an error
};

class Main {
    constructor() {
        this.background = new Background();
        this.player = new Player();
    }
};

var main = new Main();

I have tried a few things, including passing the "main" instance by reference to the constructors of the child instances, but I haven't been able to get it to work.我尝试了一些事情,包括通过引用子实例的构造函数来传递“主”实例,但我无法让它工作。 I think the code is throwing an error because the "Main" constructor hasn't finished running when the "main" instance is referenced.我认为代码会引发错误,因为引用“main”实例时“Main”构造函数尚未完成运行。

Is there any way to fix this?有没有什么办法解决这一问题?

You can pass the main instance to the Constructor of Player and Background .您可以将main实例传递给PlayerBackgroundConstructor

Then you can access it via this.main然后你可以通过this.main访问它

class Player {
    constructor(main) {
      this.main = main;
    }

    x = 10;
    y = 20;
};

class Background {
    constructor(main) {
      this.main = main;
    }

    doSomeThing() {
      const backgroundCenterX = this.main.player.x;
      const backgroundCenterY = this.main.player.y;
      console.log(backgroundCenterX, backgroundCenterY);
    }
    
};

class Main {
    constructor() {
        this.background = new Background(this);
        this.player = new Player(this);
    }

    doSomeThing() {
      this.background.doSomeThing();
    }
};

var main = new Main();
main.doSomeThing()

You can pass the main into the child constructors.您可以将 main 传递给子构造函数。 But the order matters, you have to construct and assign the player first if the background constructor is to access it on main.但是顺序很重要,如果后台构造函数要在 main 上访问它,则必须先构造和分配播放器。

 class Player { constructor(main) { this.main = main; } x = 10; y = 20; }; class Background { constructor(main) { this.main = main; this.backgroundCenterX = this.main.player.x; this.backgroundCenterY = this.main.player.y; } }; class Main { constructor() { this.player = new Player(this); this.background = new Background(this); } }; var main = new Main(); console.log(main);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在JS继承中无法从子类实例访问父类的属性? - Why I can't access a property of parent class from child class instance in JS inheritance? JS:class 的子实例如何访问另一个 class 的父实例? - JS: How can a child instance of a class access its parent instance of another class? 如何使 JavaScript 类实例成为另一个类的实例? - How can I make a JavaScript class instance be instance of another class? 如何从iframe访问父窗口中的CKEditor实例? - How can I access CKEditor instance in parent window from iframe? 如何从其他文件创建另一个类的实例 - How can I create an instance of another class from a different file 如何从类中随机选择一个实例? - How can I randomly choose one instance from a class? 如何防止Vuex干扰我的类实例? - How can I prevent Vuex from interfering with my class instance? 如何从内部 class 中访问外部 class 的实例变量? - How do I access an instance variable of an outer class from within an inner class? 如何通过单击主实例中的按钮来显示2级子组件中的数据? - How can I show data from 2 level child component by clicking a button in the main instance? 如何从传递给实例调用的“ bind”方法的预定义函数中访问对象实例的上下文? - How can I access an object instance's context from within a predefined function passed to the “bind” method invoked on the instance?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM