Js使用带有“this”关键字的ecma脚本6解构

时间:2015-09-08 07:54:10

标签: javascript ecmascript-6

我想在函数/类中使用“ this ”关键字进行解构。

我有这段代码:

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

输出是(我正在使用babel js):

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

如何实现此输出:

this._options = options;
this.title = _options.title;
this.content = _options.content;

1 个答案:

答案 0 :(得分:2)

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

如果您还想要this._options - 只需手动分配。

相关问题