Class

quokkaยท2022๋…„ 1์›” 9์ผ
0

TIL - Frontend

๋ชฉ๋ก ๋ณด๊ธฐ
6/6
post-thumbnail

๐Ÿšจ ์˜ค๋Š˜์€ class์— ๋Œ€ํ•˜์—ฌ ๊ณต๋ถ€ํ•ด๋ณด์ž,,,,

  • ์ง€๊ธˆ๊นŒ์ง€ ๋น„์Šทํ•œ ํ˜•ํƒœ์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ–ˆ์—ˆ๋Š”๋ฐ class๋ฅผ ์‚ฌ์šฉํ•ด์„œ๋„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

  • Class๋Š” ES6์— ์ถ”๊ฐ€๋œ ์ŠคํŽ™์ž…๋‹ˆ๋‹ค.

// ์ƒ์„ฑ์ž ํ•จ์ˆ˜
const User = function (name, age) {
  this.name = name;
  this.age = age;
  this.showName = function () {
    console.log(this.name);
  };
};

๐ŸŒ€ย ์ƒ์„ฑ์ž ํ•จ์ˆ˜

: ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

  • ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ํŠน์ง•
    • ๋Œ€๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•˜๊ฒŒ ๋œ๋‹ค.
    • new๋ผ๋Š” ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ• ์ˆ˜์žˆ๋‹ค
  • prototype & proto
    • ์‹ค์ œ ๊ฐ์ฑ„๋ฅผ ๋งŒ๋“ค ๋•Œ ์ƒ์„ฑ์ž์˜ prototype์ดย ์ฐธ์กฐ๋œ ๋ชจ์Šต์ด๋‹ค. ์ƒ์„ฑ์ž์˜ ํ”„๋กœํ† ํƒ€์ž…์„ ์ฐธ์กฐํ•˜๊ธฐ ๋•Œ๋ฌธ์—ย _proto_์™€ prototype์€ ๊ฐ™๋‹ค.
  • ๊ฐ„๋žตํ•˜๊ฒŒ ์ •๋ฆฌ
    • prototype์€ ์ƒ์„ฑ์ž ํ•จ์ˆ˜์— ์ •์˜ํ•œ ๋ชจ๋“  ๊ฐ์ฒด๊ฐ€ ๊ณต์œ ํ•  ์›ํ˜•

    • _proto_๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ new๋กœ ํ˜ธ์ถœํ•  ๋•Œ, ์ •์˜ํ•ด๋‘์—ˆ๋˜ prototype์„ ์ฐธ์กฐํ•œ ๊ฐ์ฒด

    • prototype์€ ์ƒ์„ฑ์ž ํ•จ์ˆ˜์— ์‚ฌ์šฉ์ž๊ฐ€ ์ง์ ‘ ๋„ฃ๋Š” ๊ฑฐ๊ณ ,ย _proto_๋Š” new๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ prototype์„ ์ฐธ์กฐํ•˜์—ฌ ์ž๋™์œผ๋กœ ๋งŒ๋“ค์–ด์ง„๋‹ค.

    • ์ƒ์„ฑ์ž์—๋Š” prototype, ์ƒ์„ฑ์ž๋กœ๋ถ€ํ„ฐ ๋งŒ๋“ค์–ด์ง„ ๊ฐ์ฒด์—๋Š”ย _proto_

    • ์ƒ์„ฑ์ž์—๋Š” prototype, ์ƒ์„ฑ์ž๋กœ๋ถ€ํ„ฐ ๋งŒ๋“ค์–ด์ง„ ๊ฐ์ฒด์—๋Š”ย _proto_

      ๐Ÿ‘‰ย  prototype์ด ๊ฐœ๋ฐœ์ž์ž…์žฅ์—์„œ ๊ฐ์ฒด์— ๋ณ€ํ™”๋ฅผ ์ฃผ๊ธฐ์œ„ํ•ด accessํ•ด์•ผ ๋  ๊ฒƒ์ด๊ณ ,ย _proto_๋Š” ์ƒ์„ฑ์ž๋กœ๋ถ€ํ„ฐ ๋งŒ๋“ค์–ด์ง„ ๊ฐ์ฒด๊ฐ€ prototype์„ ์ฐธ์กฐํ•œ ๊ฐ์ฒด์ด๋‹ค.

๐ŸŒ€ย Prototype,ย _proto_์™€ constructor์˜ ๊ด€๊ณ„

  • prototype๊ณผ constructor๋Š” ๋ถ€๋ชจ์ž์‹ ๊ด€๊ณ„๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.

class๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋งŒ๋“  ํ•จ์ˆ˜

class User2 {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }
  showName() {
    console.log(this.name);
  }
}

const tom = new User2("hoo", 26);

๐Ÿค”ย  ์ฐจ์ด์ ์€ ???!!!

  • new๋ฅผ ํ†ตํ•ด์„œ ํ˜ธ์ถœํ–ˆ์„ ๋•Œ ๋‚ด๋ถ€์—์„œ ์ •์˜ ๋œ ๋‚ด์šฉ์œผ๋กœ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค.
  • class๋ผ๋Š” ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ๋‚ด๋ถ€์— constructor๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๐Ÿ™„ย  ๊ทธ๋Ÿผ constructor๊ฐ€ ๋ฌด์—‡์ด๋ƒ?!!
    • constructor์€ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด์ฃผ๋Š” ์ƒ์„ฑ์ž ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. (new๋ฅผ ํ†ตํ•ด ํ˜ธ์ถœํ•˜๋ฉด ์ž๋™์œผ๋กœ ์‹คํ–‰๋œ๋‹ค.)
  • ๐Ÿ‘‡ย  ๊ฐ์ฒด๋ฅผ ์ดˆ๊ธฐํ™” ํ•˜๋Š” ๊ฐ’์€ ์ด๊ณณ์—์„œ ์ •ํ•ด์ง€๋Š”๋ฐ, ์ธ์ˆ˜๋กœ (name, age)๋ฅผ ๋„˜๊ฒจ ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋˜๋ฉด name๊ณผ age๊ฐ€ ๋งŒ๋“ค์–ด์ง„๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด showName์ฒ˜๋Ÿผ class๋‚ด์— ์ •์˜๋œ ๋ฉ”์„œ๋“œ๋Š” user2์— ํ”„๋กœํ† ํƒ€์ž…์— ์ €์žฅ๋œ๋‹ค.

๐Ÿค”ย  ๊ทธ๋Ÿผ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ classํ•จ์ˆ˜์™€ ๋™์ผํ•˜๊ฒŒ ๋™์ž‘ํ•˜๋„๋ก ๋งŒ๋“ค๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ??

// ์ƒ์„ฑ์ž ํ•จ์ˆ˜
const User = function (name, age) {
  this.name = name;
  this.age = age;
  // this.showName = function () {
  //   console.log(this.name);
  // };
};

User.prototype.showName = function () {
  console.log(this.name);
}
  • ์œ„์— ์ฝ”๋“œ์—์„œ //๋ถ€๋ถ„์„ ์ง€์šฐ๊ณ  ๋ฐ‘์— ์ฝ”๋“œ๋ฅผ ์ƒˆ๋กœ ์ถ”๊ฐ€ ํ–ˆ๋‹ค.

  • ๊ทธ๋Ÿผ ์ด๋ ‡๊ฒŒ ๋˜‘๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

๐Ÿค”ย  ๊ทธ๋ ‡๋‹ค๋ฉด class๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋Š” ๋ฌด์—‡์ผ๊นŒ???

const User = function (name, age) {
  this.name = name;
  this.age = age;
  // this.showName = function () {
  //   console.log(this.name);
  // };
};

User.prototype.showName = function () {
  console.log(this.name);
};

const tom = User("Tom", 30);
  • new๋ฅผ ์ œ๊ฑฐํ•œ ํ›„์— ์‹คํ–‰ํ•˜๋ฉด tom ๊ณผ undefined๊ฐ€ ๋œจ๊ฒŒ ๋œ๋‹ค.

  • class์— ๋˜‘๊ฐ™์ด new๋ฅผ ์ œ๊ฑฐํ•ด ๋ณด์•˜๋‹ค. ๐Ÿ‘‡

class User2 {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }
  showName() {
    console.log(this.name);
  }
}

const hoo = User2("Hoo", 26);

๊ทธ๋Ÿผ error๊ฐ€ ๋œจ๊ฒŒ ๋œ๋‹ค.

  • TypeError โ‡’ class๋Š” new ์—†์ด ์‹คํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค.

  • tom๊ณผ hoo๋ฅผ ๋น„๊ตํ•ด๋ณด์•˜๋‹ค.
    • ํ”„๋กœํ† ํƒ€์ž… ๋‚ด๋ถ€์— ์žˆ๋Š” constructor๋ฅผ ํ™•์ธํ•ด๋ณด๋ฉด User2์˜ constructor๋Š” class๋ผ๊ณ  ๋ช…์‹œ๋˜์–ด ์žˆ๋‹ค.
    • constructor๊ฐ€ class๋ผ๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๊ณ , ์ด ๊ฒฝ์šฐ์— new์—†์ด ํ˜ธ์ถœํ•˜๋ฉด error๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธ ํ•ด๋ณด์•˜๋‹ค.

๐ŸŒ€ย for in๋ฌธ์œผ๋กœ ํ™•์ธํ•ด๋ณด๊ธฐ

  • tom์„ for in๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ˆœํšŒํ•ด๋ณด๋ฉด name, age, ํ”„๋กœํ† ํƒ€์ž…์˜ showName๊นŒ์ง€ ๋‹ค ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋ฐ˜๋ฉด์— hoo๋ฅผ for in๋ฌธ์œผ๋กœ ํ™•์ธํ•ด๋ณด๋ฉด name, age๋งŒ ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿค”ย  ์™œ????

โ‡’ for in๋ฌธ์€ ํ”„๋กœํ† ํƒ€์ž…์— ํฌํ•จ๋œ ํ”„๋กœํผํ‹ฐ๋“ค์€ ๋‹ค ๋ณด์—ฌ์ฃผ๊ณ  ๊ฐ์ฒด๊ฐ€ ๊ฐ€์ง„ ํ”„๋กœํผํ‹ฐ๋งŒ ํŒ๋ณ„ํ•˜๊ธฐ ์œ„ํ•ด์„œ hs on ํ”„๋กœํผํ‹ฐ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ class์˜ ๋ฉ”์„œ๋“œ๋Š” for in๋ฌธ์—์„œ ์ œ์™ธ๋œ๋‹ค.

  • ๋ง‰๊ฐ„ ์ฐจ์ด์ 
    • ํด๋ž˜์Šค์— ์ •์˜๋œ ๋ฉ”์„œ๋“œ๋Š” ์—ด๊ฑฐํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค(non-enumerable)
    • for..in์œผ๋กœ ๊ฐ์ฒด๋ฅผ ์ˆœํšŒํ•  ๋•Œ, ๋ฉ”์„œ๋“œ๋Š” ์ˆœํšŒ ๋Œ€์ƒ์—์„œ ์ œ์™ธ๋ฉ๋‹ˆ๋‹ค.
    • ํด๋ž˜์Šค๋Š” ํ•ญ์ƒย ์—„๊ฒฉ ๋ชจ๋“œ๋กœ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค(use strict). ํด๋ž˜์Šค ์ƒ์„ฑ์ž ์•ˆ ์ฝ”๋“œ ์ „์ฒด์—” ์ž๋™์œผ๋กœ ์—„๊ฒฉ ๋ชจ๋“œ๊ฐ€ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค.

๐ŸŒ€ย ์ƒ์†

class์˜ ๊ฒฝ์šฐ์˜ ์ƒ์†์€ extends ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

  • Car๋ผ๋Š” class
class Car {
  constructor(color) {
    this.color = color;
    this.wheels = 4;
  }
  drive() {
    console.log("Drive...");
  }
  stop() {
    console.log("STOP!!!");
  }
}
  • Car๋ฅผ ์ƒ์†ํ•ด์„œ ๋งŒ๋“  Audi
class Audi extends Car {
  park() {
    console.log("PARK::");
  }
}

const A6 = new Audi("white");
  • extendsํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋งŒ๋“ ๋‹ค.

  • A6๋ฅผ ๊ฒ€์ƒ‰ํ•ด๋ณด๋ฉด Car class์—์„œ ์„ ์–ธํ•œ color์™€ wheels๊ฐ€ ์•ˆ์— ๋“ค์–ด์žˆ๋‹ค.
  • prototype์—๋Š” park๊ฐ€ ๋“ค์–ด์žˆ๋‹ค. class๋‚ด๋ถ€์—์„œ ์„ ์–ธํ•œ ์ด ๋ฉ”์„œ๋“œ๋Š” prototype๋ฐ‘์œผ๋กœ ๋“ค์–ด๊ฐ„๋‹ค.
  • A6.drive()๋ฅผ ์ฐพ์•„๋ณด๋ฉด A6 ๊ฐ์ฒด์—์„œ ์ฐพ๊ณ  ์—†์–ด์„œ proto์—๊ฐ€์„œ ์ฐพ๊ณ  ์—†์œผ๋‹ˆ๊น ๋˜ proto์—๊ฐ€์„œ ์ฐพ์•„๋ด์„œ drive๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

๐ŸŒ€ย  class ๋ฉ”์†Œ๋“œ ์˜ค๋ฒ„๋ผ์ด๋”ฉ (method overriding)

๐Ÿค”ย  ๋งŒ์•ฝ Audi์•ˆ์— Car์—์„œ ์ •์˜ํ•œ ๋ฉ”์„œ๋“œ์™€ ๋™์ผํ•œ ๋ฉ”์„œ๋“œ๊ฐ€ ์กด์žฌํ•˜๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ์œ ??

class Car {
  constructor(color) {
    this.color = color;
    this.wheels = 4;
  }
  drive() {
    console.log("Drive...");
  }
  stop() {
    console.log("STOP!!!");
  }
}

class Audi extends Car {
  park() {
    console.log("PARK::");
  }
  stop() {
    console.log("OFF!!");
  }
}

const A6 = new Audi("white");
  • class Audi์— stop์„ ์ถ”๊ฐ€ํ•ด๋ณด์•˜๋‹ค.

โ†’ off๊ฐ€ ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค.

  • ์ด๋ ‡๊ฒŒ ๋™์ผํ•œ ์ด๋ฆ„์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ์ •์˜ํ•  ๊ฒฝ์šฐ ๋ฎ์–ด ์“ฐ๊ฒŒ ๋œ๋‹ค.

๐Ÿค”ย  ๋งŒ์•ฝ ๋ถ€๋ชจ์˜ ๋ฉ”์„œ๋“œ๋ฅผ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์œผ๋ฉด์„œ ํ™•์žฅํ•˜๊ณ  ์‹ถ์„ ๋•Œ๋Š” ์–ด๋–ป๊ฒŒ ํ•ด์•ผ๋ ๊นŒ??

๐Ÿ™ˆย  ๊ทธ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋ฐ”๋กœ !!! super()!!!!

class Audi extends Car {
  park() {
    console.log("PARK::");
  }
  stop() {
    super.stop();
    console.log("OFF!!");
  }
}

const A6 = new Audi("white");
  • stop์•ˆ์— super๋ฅผ ์ถ”๊ฐ€ํ–ˆ๋‹ค.

โ†’ ์ด๋ ‡๊ฒŒ stop์ด ๋‚˜์˜ค๊ณ  off๊ฐ€ ๋‚˜์˜จ๋‹ค.

  • super.๋ฉ”์†Œ๋“œ๋ช…์œผ๋กœ ๋ถ€๋ชจ class์— ์ •์˜๋œ ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.
  • ์ด ๋ฐฉ์‹์„ ์˜ค๋ฒ„๋ผ์ด๋”ฉ์ด๋ผ๊ณ  ํ•œ๋‹ค.

๐ŸŒ€ย ์ƒ์„ฑ์ž ์˜ค๋ฒ„๋ผ์ด๋”ฉ

class Audi์— constructor๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋„ค๋น„๊ฒŒ์ด์…˜์„ ์ถ”๊ฐ€ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

class Audi extends Car {
  constructor() {
    this.navigation = 1;
  }
  park() {
    console.log("PARK::");
  }
}

const A6 = new Audi("white");

์ด๋ ‡๊ฒŒ ์ €์žฅํ•˜๋ฉด ERROR๊ฐ€ ๋‚œ๋‹ค. ใ…‹ใ…‹ใ…‹ใ…‹

  • constructor์—์„œ this๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์ „์— super constructor ์ฆ‰, ๋ถ€๋ชจ ์ƒ์„ฑ์ž๋ฅผ ๋ฐ˜๋“œ์‹œ ๋จผ์ € ํ˜ธ์ถœํ•ด์•ผํ•œ๋‹ค.
  • class์— constructor ์€ {} ๋นˆ ๊ฐ์ฒด๋กœ ๋งŒ๋“ค์–ด์ฃผ๊ณ  this๋กœ ์ด ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฅด์ณ์ค˜์•ผํ•œ๋‹ค.
  • ๋ฐ˜๋ฉด extends๋ฅผ ์‚ฌ์šฉํ•˜์„œ ๋งŒ๋“  ์ž์‹ class๋Š” ๋นˆ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ณ  this์— ํ• ๋‹นํ•ด์ฃผ๋Š” ์ด ๊ณผ์ •์„ ๊ฑด๋„ˆ ๋›ธ ์ˆ˜ ์žˆ๋‹ค.
  • ๊ทธ๋ž˜์„œ ํ•ญ์ƒ superํ‚ค์›Œ๋“œ๋กœ ๋ถ€๋ชจ class์˜ constructorํ‚ค์›Œ๋“œ๋ฅผ ์‹คํ–‰ํ•ด์ค˜์•ผํ•จ.
class Audi extends Car {
  super();
  constructor() {
    this.navigation = 1;
  }
  park() {
    console.log("PARK::");
  }
}

const A6 = new Audi("white");

๋‹ค์‹œ A6๋ฅผ ์ฝ˜์†”์ฐฝ์— ํ™•์ธํ•ด๋ณด๋ฉด

โ†’ navigation ์€ 1๋กœ ์ž˜ ๋œจ์ง€๋งŒ, color์€ undefined๊ฐ€ ๋œฌ๋‹ค. ์™œ???!!!!

  • ์ž์‹ class์˜ constructor์— ๋ถ€๋ชจ์™€ ๋™์ผํ•œ ์ธ์ˆ˜๋ฅผ ๋ฐ›๋Š” ์ž‘์—…์„ ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.
class Audi extends Car {
  constructor(color) {
    super(color);
    this.navigation = 1;
  }
  park() {
    console.log("PARK::");
  }
}
const A6 = new Audi("white");

constructor์— color์„ ๋ฐ›๊ณ  super์— color์„ ๋„˜๊ฒจ์ค˜์•ผ ํ•œ๋‹ค.

โ†’ ๊ทธ๋Ÿผ ์ž˜ ๋“ค์–ด๊ฐ€์ง„๋‹ค.!!!!

์ •๋ฆฌ!!

class Car {
  constructor(color) {
    this.color = color;
    this.wheels = 4;
  }
  drive() {
    console.log("Drive...");
  }
  stop() {
    console.log("STOP!!!");
  }
}

class Audi extends Car {
  park() {
    console.log("PARK::");
  }
}
const A6 = new Audi("white");
  • ์ฒ˜์Œ์— ์ด ์ฝ”๋“œ์˜€๋‹ค. ์ž์‹ class์˜ Audi์—๋Š” constuctor๊ฐ€ ์—†๋‹ค. ์ด๋Ÿด๋•Œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š”???!!!
class Car {
  constructor(color) {
    this.color = color;
    this.wheels = 4;
  }
  drive() {
    console.log("Drive...");
  }
  stop() {
    console.log("STOP!!!");
  }
}

class Audi extends Car {
	constructor(...args){
		super(...args);
}
  park() {
    console.log("PARK::");
  }
}

const A6 = new Audi("white");
  • constructor๊ฐ€ ์—†์œผ๋ฉด constructor๊ฐ€ ์žˆ๋Š”์ฒ™ ํ–‰๋™ํ•œ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์ž์‹ ์ƒ์„ฑ์ž๋Š” ๋ฌด์กฐ๊ฑด ๋ถ€๋ชจ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•ด์•ผํ•œ๋‹ค.
  • constructor๋งŒ ์‚ฌ์šฉํ•˜๋ฉด ์ฒ˜๋ฆฌ๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ํ•ญ์ƒ super๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ˜ธ์ถœํ•ด์ค˜์•ผ ํ•œ๋‹ค.
  • ๊ทธ ํ›„์— this.property๋กœ ํ• ๋‹นํ•ด์ค˜์•ผ ํ•œ๋‹ค.
profile
๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป ๋งค์ผ๋งค์ผ์ด ๊ธฐ๋Œ€๋˜๋Š” ๊ฐœ๋ฐœ์ž ^^

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