javascript es6 기초 배우기 006 - Class

최대 1 분 소요

Class

  • javascript에서 class는 함수처럼 쓰임
class Person {
  constructor(region_, gender_) {
    this.region = region_;
    this.gender = gender_;
  }
  greeting(val='an-nyeong') {
    console.log(val);
  }
}

let korean = new Person('Korea', 'male');
console.log(korean);
/* Result
Person {
  region: 'Korea',
  gender: 'male'
}
*/

korean.gender = 'female';
console.log(korean);
/* Result
Person {
  region: 'Korea',
  gender: 'female'
}
*/

korean.greetings();
/* Result
an-nyeong
*/

class American extends Person {
  constructor(region_, gender_, language_) {
    super(region_, gender_);
    this.language = language_;
  }
  greetings(val = 'Hello') {

  }
}
let american = new American('USA', 'female', 'English');
console.log(american);
/* Result
Person {
  region: 'USA',
  gender: 'female',
  language: 'English'
}
*/

console.log(greetings);
/* Result
Hello
*/

참고