最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get a class from the constructor of an anonymous class inside it in JS - Stack Overflow

programmeradmin5浏览0评论

I have this situation in JS:

class Outer{
    constructor() {
        this.fruit = "orange";
        let inner = new this.Inner();
    }
    Inner = class{
        constructor() {
            // here
        }
    }
}
let outer = new Outer();

From the constructor of the class Inner, how can I get the current instance of Outer, as if this was used in the outer constructor?

Sure, I could pass the outer class as a parameter into the inner class' constructor, or put the outer class in a variable, but is there any other method?

I have this situation in JS:

class Outer{
    constructor() {
        this.fruit = "orange";
        let inner = new this.Inner();
    }
    Inner = class{
        constructor() {
            // here
        }
    }
}
let outer = new Outer();

From the constructor of the class Inner, how can I get the current instance of Outer, as if this was used in the outer constructor?

Sure, I could pass the outer class as a parameter into the inner class' constructor, or put the outer class in a variable, but is there any other method?

Share Improve this question edited 16 hours ago VLAZ 29.1k9 gold badges62 silver badges84 bronze badges asked 17 hours ago AnpangAnpang 131 silver badge3 bronze badges New contributor Anpang is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 17
  • How do you define "clean method"? Passing the outer class as argument is usually considered the clean method. – jabaa Commented 17 hours ago
  • When I pass it as an argument, like new outer.Inner(outer);, I have to put outer twice. Sometimes it's just a variable name, but sometimes it's a long expression, so I personally don't consider that as clean. – Anpang Commented 17 hours ago
  • But that's your opinion. Opinion-based questions are off-topic. That's why I ask for a technical specification of "clean". In my opinion it's clean because it's the common pattern. It's the common pattern, because AFAIK there is no other way. – jabaa Commented 17 hours ago
  • 1 I've read your rude comment that was deleted and I have to admit that I don't understand it. The common pattern is to pass the instance as argument because there is no other way. I've wrote it in two comments. It's not Stack Overflow's fault that JavaScript works how it works or that you don't like the answer. As you can see in another comment, clarification is important, because another user misunderstood your question (or answered your wrong description). – jabaa Commented 16 hours ago
  • 2 That's not a pick. That's two different answers. If you want to access the class, you can use Outer. It's accessible. If you want to access the instance, you have to pass it as argument. TBH, I don't even see what you criticize here. You were asked for clarification and got your answer. What exact statement is a problem? – jabaa Commented 16 hours ago
 |  Show 12 more comments

1 Answer 1

Reset to default 1

You can use an IIFE to create the class and give it the current instance:

class Outer{
    constructor() {
        this.fruit = "orange";
        let inner = new this.Inner();
    }
    
    Inner = (instance => class{
        constructor() {
            console.log(instance instanceof Outer);
            console.log(instance.fruit);
        }
    })(this)
}
let outer = new Outer();

However, a more idiomatic approach is to accept the instance in the constructor of Inner. Then the class can be separated from Outer and Outer can expose a method that handles the construction of Inner which can create many instances of it:

class Inner {
    constructor(instance) {
        console.log(instance instanceof Outer);
        console.log(instance.fruit);
    }
}

class Outer{
    constructor() {
        this.fruit = "orange";
        let inner = this.makeInner();
    }
    
    makeInner() {
      return new Inner(this);
    }
}
let outer = new Outer();

const secondInnerInstance = outer.makeInner();

Or alternatively consider breaking the circular dependency between the two and introducing a factory to handle the more complex construction.

发布评论

评论列表(0)

  1. 暂无评论