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,237
  • 6
  • 36
  • 59

1 Answers1

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

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

zerkms
  • 240,587
  • 65
  • 429
  • 525
  • 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