2017-03-22 1 views
-1

여기 destructuring 작동하는 방법에 개체를 desctructure합니다 :방법이

let o = {a:0, b:1}; 
let {a, b} = o; 
console.log(a,b); 

어떻게 this에 B, 속성을 할당하는 방법은 무엇인가? 예를 들어이 같은 상황에서

:

class myClass { 
    constructror(){ 
     this.a = null; 
     this.b = null; 
    } 
    myMethod(o){ 
     //destructure o here, so that 'this' was assigned a and b 
    } 
} 
const o = {a:0, b:1}; 
const myInstance = new myClass(); 
myInstance.myMethod(o); 
+0

@AndrewLi, 죄송합니다, –

답변

3
let o = {a: 0, b: 1}; 
Object.assign(this, o); 
+0

감사합니다, 그것은 나를 위해 작동 일부 누락 된 부분을 추가하지만, 같은 구문 일이 있다면 나는 생각했다 'this {a, b} = o; ' –

+1

정말로. 객체를 해체함으로써 값을 별도의 변수로 나눠서 각 변수를 'this'에 할당해야합니다. – fubar