본문 바로가기

프론트엔드 공부

class의 개념 이해하기 (생성자함수>class 변경)

//**생성자함수를 class로 변경하기 (ES6)
//javascript는 프로토타입의 프로그램언어인데 안정적이고 신뢰도가 높은 class 개념을 다른 언어에서 착안하여 직관적이고 간결하게 활용도 높게 나타낸다

// function User(first,last){
//   this.firstName = first;
//   this.lastName = last;
// }
// User.prototype.getFullName = function(){
//   return `${this.firstName} ${this.lastName}`;
// }

class User {
  //내부함수constructor(매개변수a,b)
  //constructor:function(){} 의 축약형임
  constructor(first,last){
    this.firstName = first;
    this.lastName = last;
  }
  getFullName(){
   return `${this.firstName} ${this.lastName}`;    
  }
}

const mag = new User('mag','hwang');
const amy = new User('amy','choi');
const pheter = new User('pheter','park');

console.log(mag.getFullName());