Hacker News new | past | comments | ask | show | jobs | submit login

Working with classes in ES6 is such a relief. My only complaint is that if you save a method as a variable or pass it to another function, it'll just be a function with its own scope (`this` won't reference the class instance, but instead the method function).

Easily "fixed" if you remember to put `.bind(this)` everywhere, though.




Or use :

  myMethod = (arg) => {return this.value + arg;}
With es7.classProperties in babel. https://babeljs.io/docs/usage/experimental/


Or more tersely:

  myMethod = arg => this.value + arg;


or depending on what you are doing in that method, use a get myProperty() {//do stuff and return prop}


Sample code from just this afternoon:

      const typeMap = {
        'i-text': this.processText.bind(this),
        'image': this.processImage.bind(this),
        'rect': this.processRectangle.bind(this)
      };
Really annoying if you ask me, though I understand why it had to be this way.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: