How to self-reference ES6 class instance (this in ES5)

By gattermeier

My problem: I am instantiating a class (here named ExternalClass) inside a different class (named SomeClass below). The tricky part is that ExternalClass expect a reference to the class object it has been called from: the SomeClass instance. In ES5 I would just pass ‘this’, which doesn’t work in ES6 (transpiled with babel).

class ExternalClass {
  constructor(someClassInstance, variable) {
    this.someClassInstance = someClassInstance;
    this.variable = variable;
  }
}
class SomeClass {
  constructor() {
  }
  doSomething() {
    let test = new ExternalClass(this, variable) // this is undefined
  }
}

How can I pass the SomeClass instance to ExternalClass from doSomething() inside SomeClass?

Source: Stack Overflow

    

Share it with your friends!

    Fatal error: Uncaught Exception: 12: REST API is deprecated for versions v2.1 and higher (12) thrown in /home/content/19/9652219/html/wp-content/plugins/seo-facebook-comments/facebook/base_facebook.php on line 1273