๐Ÿ”ฎ Javascript [class] ์ดํ•ดํ•˜๊ธฐ

Kyla Kimยท2023๋…„ 7์›” 10์ผ

ํ”„์—”๊ฟˆ๋‚˜๋ฌด

๋ชฉ๋ก ๋ณด๊ธฐ
7/8

[Javascript] ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ Class ์‚ฌ์šฉํ•˜๊ธฐ-constructor, extends, super ์‚ฌ์šฉ๋ฒ•

๐Ÿ”ฎ Class ๐Ÿ”ฎ

  • ES6์—์„œ๋ถ€ํ„ฐ ์ง€์›
  • ์ต์Šคํ”Œ๋กœ๋Ÿฌ์—์„œ๋Š” ์‚ฌ์šฉ ๋ถˆ๊ฐ€
  • ์žฌ์‚ฌ์šฉ์„ฑ ๐Ÿ‘

โœจ class ๊ธฐ๋ณธ ๋ฌธ๋ฒ•

class ์ƒ์„ฑํ•˜๊ธฐ

class Person {
}

let kim = new Person();
console.log(kim);

class๋กœ ๋งŒ๋“ค์–ด์ค€ ์˜ˆ์‹œ Person ์ด๋ผ๋Š” ์ด๋ฆ„์˜ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค

class ์ดˆ๊ธฐ๊ฐ’ ์„ค์ •ํ•ด์ฃผ๊ธฐ

Constructor(์ƒ์„ฑ์ž)๋ฅผ ์ด์šฉํ•˜๋ฉด class ๊ฐ์ฒด์˜ ์ดˆ๊ธฐ๊ฐ’์„ ์„ค์ •ํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค.
class ๋‚ด๋ถ€์—์„œ constructor๋Š” ํ•œ ๊ฐœ๋งŒ ์กด์žฌํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, 2๊ฐœ ์ด์ƒ ์‚ฌ์šฉํ•˜๋ฉด Syntax Error๊ฐ€ ๋ฐœ์ƒํ•จ

class Person{
	constructor(name,age,city){
      this.name = name;
      this.age = age;
      this.city = city;
    }
}

let kim = new Person('Kim', '24', 'Seoul');

console.log(kim);

constructor

[JavaScript] ES6 Class์˜ ์ƒ์„ฑ์ž(constructor) ํ•จ์ˆ˜
๊ฐ์ฒด(์ธ์Šคํ„ด์Šค)๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ํด๋ž˜์Šค ํ•„๋“œ๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๊ธฐ ์œ„ํ•œ ํŠน์ˆ˜ํ•œ ๋ฉ”์†Œ๋“œ.
โ€ป ํด๋ž˜์Šค ํ•„๋“œ : ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ์ƒ์„ฑ์ž ํ•จ์ˆ˜์—์„œ this์— ์ถ”๊ฐ€ํ•œ ํ”„๋กœํผํ‹ฐ

constructor๋Š” ์ƒˆ๋กœ์šด ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ๊ฐ€์žฅ ์ฒ˜์Œ ์‹คํ–‰๋˜๋ฉฐ ์ดˆ๊ธฐ๊ฐ’์„ ์„ค์ •ํ•ด์ค€๋‹ค.

// ํด๋ž˜์Šค ์„ ์–ธ๋ฌธ
class Person {
  // constructor(์ƒ์„ฑ์ž). ์ด๋ฆ„์„ ๋ฐ”๊ฟ€ ์ˆ˜ ์—†๋‹ค.
  constructor(name) {
    // this๋Š” ํด๋ž˜์Šค๊ฐ€ ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
    // _name์€ ํด๋ž˜์Šค ํ•„๋“œ์ด๋‹ค.
    this._name = name;
  }
}
 
// ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
const me = new Person('Lee');
console.log(me); // Person {_name: "Lee"}

ํด๋ž˜์Šค๋Š” constructor๋ผ๋Š” ์ด๋ฆ„์„ ๊ฐ€์ง„ ํŠน๋ณ„ํ•œ ๋ฉ”์„œ๋“œ๋ฅผ ํ•˜๋‚˜์”ฉ๋งŒ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค. 2๊ฐœ ์ด์ƒ์ผ ์‹œ SyntaxError์„ ์œ ๋ฐœํ•œ๋‹ค.

์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•  ๋•Œ new ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ํ˜ธ์ถœํ•œ ๊ฒƒ์ด ๋ฐ”๋กœ constructor์ด๋ฉฐ constructor์˜ ํŒŒ๋ผ๋ฏธํ„ฐ์— ์ „๋‹ฌํ•œ ๊ฐ’์€ ํด๋ž˜์Šค ํ•„๋“œ์— ํ• ๋‹นํ•œ๋‹ค.

constructor์€ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ๋‹ค. ์ƒ๋žตํ•˜๋ฉด ํด๋ž˜์Šค์— constructor(){ }๋ฅผ ํฌํ•จํ•œ ๊ฒƒ๊ณผ ๋™์ผํ•˜๊ฒŒ ๋™์ž‘ํ•œ๋‹ค.

๋”ฐ๋ผ์„œ ์ธ์Šคํ„ด์Šค์— ํ”„๋กœํผํ‹ฐ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ ค๋ฉด ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•œ ์ดํ›„ ํ”„๋กœํผํ‹ฐ๋ฅผ ๋™์ ์œผ๋กœ ์ถ”๊ฐ€ํ•ด์•ผ ํ•œ๋‹ค.

class Person { }
 
const me = new Person();
console.log(me); // Person {}
 
// ํ”„๋กœํผํ‹ฐ ๋™์  ํ• ๋‹น ๋ฐ ์ดˆ๊ธฐํ™”
me._name = 'daeun';
console.log(me); // Person {_name: "daeun"}

constructor๋Š” ์ธ์Šคํ„ด์Šค์˜ ์ƒ์„ฑ๊ณผ ๋™์‹œ์— ํด๋ž˜์Šค ํ•„๋“œ์˜ ์ƒ์„ฑ๊ณผ ์ดˆ๊ธฐํ™”๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.
๋”ฐ๋ผ์„œ ํด๋ž˜์Šค ํ•„๋“œ๋ฅผ ์ดˆ๊ธฐํ™”ํ•ด์•ผ ํ•œ๋‹ค๋ฉด constructor๋ฅผ ์ƒ๋žตํ•ด์„œ๋Š” ์•ˆ๋œ๋‹ค.

class Person {
  constructor(name) {
    this._name = name;
  }
}
 
const me = new Person('daeun');
console.log(me); 

class ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉํ•˜๊ธฐ

class์—์„œ ์„ค์ •ํ•œ ์ดˆ๊ธฐ๊ฐ’์„ ์ ‘๊ทผํ•ด ํŠน์ • ๊ธฐ๋Šฅ์„ ํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค
class ์•ˆ์— function ํ˜•์‹์œผ๋กœ ๋งŒ๋“ค์–ด์ค€ ๋’ค ํ•ด๋‹น ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋œ๋‹ค

๋‚ด๋…„์— ํ•ด๋‹น ์‚ฌ๋žŒ์ด ํ•œ ์‚ด ๋” ๋จน๋Š”๋‹ค๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ class ์•ˆ์— ์ •์˜ํ•œ ๋’ค ํ˜ธ์ถœํ•ด๋ณด๋ฉด

class Person{
	constructor(name,age,city){
 	this.name = name;
 	this.age = age;
 	this.city = city;
	}
  //๋ฉ”์†Œ๋“œ ์ƒ์„ฑ
  nextYearAge(){
  	return Number(this.age) + 1;
  }
}
let kim = new Person('kim','24','Seoul');

console.log(kim.nextYearAge());

class๋Š” javascript์ƒ ๊ฐ์ฒด ์˜ ํ˜•ํƒœ์ด๋ฏ€๋กœ ์ƒ์„ฑ๋œ class ๊ฐ์ฒด์— class ๋ฐ–์—์„œ ์ƒˆ๋กœ์šด ๋ฉ”์†Œ๋“œ๋ฅผ ๋„ฃ๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค

class Person{
	constructor(name,age,city){
 	this.name = name;
 	this.age = age;
 	this.city = city;
	}
  //๋ฉ”์†Œ๋“œ ์ƒ์„ฑ
  nextYearAge(){
  	return Number(this.age) + 1;
  }
}
let kim = new Person('kim','24','Seoul');

kim.eat = function(){
	return 'apple'
}

console.log(kim.nextYearAge());
console.log(kim.eat());

ํ•˜์ง€๋งŒ ์ด๋ ‡๊ฒŒ ๋ฐ–์—์„œ ์ถ”๊ฐ€ํ•œ class๋Š” ๋‚˜์ค‘์— ์ƒˆ๋กœ์šด new Person class๋กœ ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๋•Œ๋Š” ํ˜ธ์ถœํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค

class Person {
    constructor (name,age,city) {
        this.name = name;
        this.age = age;
        this.city = city;
    }
    //๋ฉ”์†Œ๋“œ์ƒ์„ฑ
    nextYearAge() {
        return Number(this.age) + 1;
    }
}

let kim = new Person('Kim','24','Seoul');

kim.eat = function () {
    return 'apple'
}

console.log('๊น€์”จ ๋‚ด๋…„์—๋Š” ๋ช‡์‚ด์ธ๊ฐ€์š” ?' + kim.nextYearAge());
console.log('๊น€์”จ๊ฐ€ ๋จน์€๊ฑด? ' + kim.eat());

let park = new Person('Park', '31', 'busan');

console.log('๋ฐ•์”จ ๋‚ด๋…„์—๋Š” ๋ช‡์‚ด์ธ๊ฐ€์š”?' + park.nextYearAge());
console.log('๋ฐ•์”จ๊ฐ€ ๋จน์€๊ฑด?' + park.eat());

console ๊ฒฐ๊ณผ

park class์—์„œ eat() ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ ์‹คํ–‰๋˜์ง€ ์•Š๋Š” ๋ชจ์Šต

โœจ ์ƒ์†(extends)

class์—์„œ ์ƒ์†๊ฐœ๋…์„ ์ด์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

class Person {
    constructor (name,age,city) {
        this.name = name;
        this.age = age;
        this.city = city;
    }
    //๋ฉ”์„œ๋“œ์ƒ์„ฑ
    nextYearAge() {
        return Number(this.age) + 1;
    }
}

class introducePerson extends Person {
    introduce () {
        return `์ €๋Š” ${this.city}์— ์‚ฌ๋Š” ${this.name} ์ž…๋‹ˆ๋‹ค.`
    }
}

let kim = new introducePerson('Kim','24','Seoul');

console.log(kim.introduce());

introducePersonํด๋ž˜์Šค์—์„œ Person์„ ์ƒ์†๋ฐ›์•˜๊ธฐ ๋•Œ๋ฌธ์—
this.city์™€ this.name์„ ๋ชจ๋‘ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

โœจ super ์‚ฌ์šฉํ•˜๊ธฐ

introducePerson ํ•˜์œ„ํด๋ž˜์Šค์—์„œ ๊ธฐ์กด class์˜ ๊ฐ’์„ ๊ฐ€์ ธ๋‹ค ์“ฐ๋Š” ๊ฑด ์ข‹์•˜์ง€๋งŒ,
์ถ”๊ฐ€์ ์œผ๋กœ introducePerson์ด๋ผ๋Š” ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ๋งŒ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์€ ๊ฐ’์ด ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค.

์ด ๋•Œ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ด super๋ผ๋Š” ํ‚ค์›Œ๋“œ์ด๋ฉฐ,
์ด๋Š” ๊ฐ์ฒด์˜ ๋ถ€๋ชจ๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค.

์ž์‹ ์ชฝ์— ์ถ”๊ฐ€์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ดˆ๊ธฐ๊ฐ’์ด ํ•„์š”ํ•  ๊ฒฝ์šฐ constructor์— super๋กœ ๋ถ€๋ชจ ์ดˆ๊ธฐ๊ฐ’์„ ์„ธํŒ…ํ•œ ๋’ค ์ž์‹ class์—์„œ๋งŒ ์‚ฌ์šฉํ•  ์ดˆ๊ธฐ๊ฐ’๋งŒ ๋”ฐ๋กœ ์ง€์ •ํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•˜๋ฉฐ super ๊ธฐ๋Šฅ์„ ์ด์šฉํ•ด์„œ ์ž์‹ class์—์„œ ๋ถ€๋ชจ ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

class Person {
    constructor (name,age, city) {
        this.name = name;
        this.age = age;
        this.city = city;
    }
    //๋ฉ”์„œ๋“œ์ƒ์„ฑ
    nextYearAge() {
        return Number(this.age) + 1;
    }
}

class introducePerson extends Person {
    constructor(name, age, city, futureHope) {
        super(name, age, city);
        this.futureHope = futureHope
    }
    introduce () {
        return `์ €๋Š” ${this.city}์— ์‚ฌ๋Š” ${this.name} ์ž…๋‹ˆ๋‹ค. 
            ๋‚ด๋…„์—” ${super.nextYearAge()}์‚ด์ด๋ฉฐ,
            ์žฅ๋ž˜ํฌ๋ง์€ ${this.futureHope} ์ž…๋‹ˆ๋‹ค.`
    }

}

let kim = new introducePerson('Kim','24','Seoul', '๊ฐœ๋ฐœ์ž');

console.log(kim.introduce());

class๋ฅผ ์ด์šฉํ•  ๊ฒฝ์šฐ ๊ทœ์น™์„ฑ์„ ๊ฐ–๋Š” ๊ฐ์ฒด๋ฅผ ์ผ๊ด€์„ฑ ์žˆ๊ฒŒ ๋งŒ๋“œ๋Š” ๊ฒŒ ๊ฐ€๋Šฅํ•˜๋ฉฐ,
์ƒ์†์„ ํ†ตํ•ด์„œ ๊ธฐ๋Šฅ ํ™•์žฅ์ด ์šฉ์ดํ•˜๋‹ค๋Š” ๊ฒƒ ์•Œ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

profile
The only way of discovering the limits of the possible is to venture a little way past them into the impossible.

0๊ฐœ์˜ ๋Œ“๊ธ€