4

I want to use destructuring with the "this" keyword inside function/class.

I have this code:

class Test {
  constructor( options ) {
    let {title, content} = options;
  }
}

The output is (i am using babel js):

var _options = options;
var title = _options.title;
var content = _options.content;

How can i achieve this output:

this._options = options;
this.title = _options.title;
this.content = _options.content;
Bazinga
  • 10,716
  • 6
  • 38
  • 63

1 Answers1

4
class Test {
  constructor( options ) {
    ({title: this.title, content: this.content} = options);
  }
}

If you want the this._options additionally - just assign it manually.

zerkms
  • 249,484
  • 69
  • 436
  • 539
  • 4
    Assuming you want to bring in all properties of `options`, you could also do `Object.assign(this, this._options = options)`. –  Sep 08 '15 at 08:30