접근자프로퍼티, computed property

lee jae hwan·2022년 7월 30일

javascript

목록 보기
63/107
class User{
  _name='';
  constructor(name){
    this._name=name;    
  }
  get name(){
    return this._name;
  }
  ['u'+2](){
    console.log(this._name);
  }
  [Symbol.iterator](){

  }
}

let user = new User('lee');
user.u2();

접근자프로퍼티, computed property는 객체와 class에서 사용가능하고 생성자함수에서는 사용할 수 없다.



let arg = 'jh';
class User{
    _name='jh';
    constructor(){
        console.log(this._name);
    }
    ['get'+arg]=()=>this._name;
    ['get'+this._name]=()=>this._name; // 에러
}

let user = new User();
console.log(user.getjh);

computed property에 ['get'+this._name]와 같이 this를 사용할 수 없다.
this는 constructor 완료된상태에서 접근가능한것이지 완성되기전에 접근되지 않는다.

0개의 댓글