解构 ecmascript es7

destructuring ecmascript es7

有没有办法简化下面的代码?

let { page_size, current_page } = paging;

this.p_start = page_size * current_page;
this.p_current = current_page;
this.p_dg_current = this.p_current + 1;
this.p_size = page_size;

paging 是一个对象,具有属性 page_sizecurrent_page

有没有一种破坏性的方法可以将paging.page_size和paging.current_page直接分配给this.p_size和this.p_current?

  let { this.p_size = page_size, this.p_current = current_page } = paging;

  this.p_start = page_size * current_page;
  this.p_dg_current = this.p_current + 1;

你可以这样试试吗

 let { page_size: p_size, current_page: p_current } = paging;
        this.p_start = page_size * current_page;
        this.p_dg_current = this.p_current + 1;

解构的语法是{ propertyName: targetExpression = default }。不过你的想法是对的(you need to omit the let,因为你没有声明变量):

({ page_size: this.p_size, current_page: this.p_current } = paging);

this.p_start = this.p_size * this.p_current;
this.p_dg_current = this.p_current + 1;

像这样尝试,而不是 paging1 使用 this

let paging = {page_size : 10, current_page : 9};
let paging1 = {p_size : 0, c_size : 0};
({ page_size : paging1.p_size, current_page : paging1.c_size } = paging)

console.log(paging1.p_size+ ' '+ paging1.c_size)