๐ Class
์๋ฐ์คํฌ๋ฆฝํธ ์์ฒด๋ ํ๋กํ ํ์
์ ๊ธฐ๋ฐ์ผ๋ก ๊ฐ์ฒด์งํฅ์ ์ง์ํ๋ ์ธ์ด๋ค. ํด๋์ค๋ ํด๋์ค์ ์ต์ํ ๊ฐ๋ฐ์๋ค์ ์ํ ํ๋กํ ํ์
์ ๋ฌธ๋ฒ์ ์คํ์ด๋ผ๊ณ ๋ถ๋ฆฐ๋ค. ํ์ง๋ง ํด๋์ค๊ฐ ์๋ฐ์คํฌ๋ฆฝํธ ๋ด์์ ๋์ํ๋ ๋ฐฉ์์ ์ฐจ์ด์ ์ด ์กด์ฌํ๋ค. ํด๋์ค์ ๋ํด์ ์์๋ณด์
๐ ํด๋์ค์ ์ ์ฒด์ ํธ์ด์คํ
๋จผ์ ํด๋์ค๋ ์๋ฐ์คํฌ๋ฆฝํธ์์ ํจ์๋ค. ์๋ฐ์คํฌ๋ฆฝํธ์์ ํจ์๋ ์ผ๊ธ๊ฐ์ฒด์ด๋ฏ๋ก ๋งค๊ฐ๋ณ์, ๋ฐํ ๊ฐ์ผ๋ก ์ฌ์ฉ์ด ๊ฐ๋ฅํ๊ณ ๋ณ์์ ์ ์ฅํ ์๋ ์๋ค. ์ด๋ ๊ฒ ์์ฑ์ ํจ์์ ๊ฐ์ด ํจ์์ง๋ง ์ฐจ์ด์ ์ด ์กด์ฌํ๋ค. ์์ฑ์ ํจ์๋ new๊ฐ ์์ด ํธ์ถ ์์ ํจ์๋ก์จ ๋์ํ์ง๋ง, ํด๋์ค๋ new ํค์๋์์ด ํธ์ถํ ์ ์๋ค.
class Cat {
constructor() {
this.name = "์ผ์น์ด"
}
call() {
console.log(this.name)
}
}
console.log(Cat()) //TypeError: Class constructor Cat cannot be invoked without 'new'์ ์ฝ๋์์ new์์ด ํธ์ถ์ ์๋ฌ๊ฐ ๋ฐ์ํ ๊ฒ์ ๋ณผ ์ ์๋ค.
ํด๋์ค๋ ํจ์์ด๊ธฐ ๋๋ฌธ์ ํธ์ด์คํ ์ด ๋ฐ์ํ์ง๋ง, const, let์ผ๋ก ํจ์ํํ์๊ณผ ๊ฐ์ด ์ด๊ธฐํ ์ ๊น์ง ํธ์ถ์ด ๋ถ๊ฐ๋ฅํ TDZ์ ๋น ์ง๋ ํน์ง์ ๊ฐ์ง๋ค.
const Cat = ""
{
console.log(Cat) // ReferenceError: Cannot access 'Cat' before initialization
class Cat {}
}์ ์ฝ๋์์ ํธ์ด์คํ
๋์ง ์์๋ค๋ฉด ์ ์ญ์์ ๋ง๋ Cat ๋ณ์๊ฐ ์๊ธฐ ๋๋ฌธ์ " "๋ก ์ฝ์์ ๋์์ผํ๋ค. ํ์ง๋ง ํธ์ด์คํ
์ด ์ผ์ด๋ ์๋ฌ๊ฐ ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
๐ ํด๋์ค์ ๋ฉ์๋
ํด๋์ค๋ ์์ฑ์ ํจ์์ ๊ฐ์ด ์ธ์คํด์ค ๋ฉ์๋, ์ ์ ๋ฉ์๋, ํ๋กํ ํ์ ๋ฉ์๋, ์ด ์ธ๊ฐ์ง ๋ฉ์๋ ์์ญ์ ๊ฐ์ง๋ค. ๊ฐ๊ฐ์ ๋ํด ์์๋ณด์.
constructor
constructor ๋ฉ์๋๋ class๋ก ๋ง๋ค ์ธ์คํด์ค๋ฅผ ์์ฑํ๊ณ ์ด๊ธฐํํ๊ธฐ ์ํ ๋ฉ์๋๋ค. ํด๋์ค๋ ํจ์์ด๊ธฐ ๋๋ฌธ์ ํ๋กํ ํ์
์ ๊ฐ์ง๋๋ฐ ์ด๋ ํ๋กํ ํ์
์ constructor๋ ํด๋์ค ์์ ์ ๊ฐ๋ฆฌํค๊ณ ์๋ค.
์ด๋ this๋์ ๋ค๋ฅธ ๊ฐ์ฒด๋ฅผ returnํ๋ฉด ๊ฒฐ๊ณผ๊ฐ ๋ฌ๋ผ์ง๊ธฐ ๋๋ฌธ์ return์ ์๋ตํ๋ ๊ฒ ์ข๋ค.
class Cat {
constructor(name) {
this.name = name
return {}
}
}
const cat = new Cat("์์น")
console.log(cat) // {}ํด๋์ค๋ก ๋ง๋ค์ด์ง ์ธ์คํด์ค๋ ์์ฑ์ํจ์๊ฐ ๋ง๋ ์ธ์คํด์ค์ ๋์ผํ๊ฒ ํ๋กํ ํ์ ์ฒด์ธ์ ๋ค์ด๊ฐ๊ฒ ๋๋ค. ๊ฒฐ๊ตญ ์ ๋ฆฌํ๋ฉด ํ๋กํ ํ์ ์ฒด์ธ์์ ํด๋์ค๋ ์ธ์คํด์ค๋ฅผ ์์ฑํ๋ ์์ฑ์ ํจ์์ ๊ฐ์ ์ญํ ์ ํ๋ค๊ณ ์๊ฐํ ์ ์๋ค.
ํ๋กํ ํ์ ๋ฉ์๋
์์ฑ์ํจ์์์๋ ์ธ์คํด์ค๋ค์ด ๊ณตํต์ผ๋ก ์ฐธ์กฐํ ํจ์๋ฅผ ๋ง๋ค ๋, ํ๋กํ ํ์ ์ ํจ์๋ฅผ ์ ๋ฌํ์ง๋ง ํด๋์ค์์๋ ํด๋์ค ๋ด๋ถ์ ๋ฉ์๋๋ฅผ ์ ์ธํ๋ฉด ์๋์ผ๋ก ํ๋กํ ํ์ ์ ๋ฉ์๋๊ฐ ๋๋ค.
class Cat {
constructor(name) {
this.name = name // ์ธ์คํด์ค ๋ฉ์๋
}
call() {
//ํ๋กํ ํ์
๋ฉ์๋
console.log("์ผ์น")
}
}
const cat = new Cat("์ผ์น์ด")
cat.call() // ์ผ์น
cat.__proto__.call("์ผ์น")์ ์ ๋ฉ์๋
์ ์ ๋ฉ์๋๋ ์์ฑ์ํจ์์์ ์์ฑ์ํจ์๊ฐ์ฒด ์์ฒด๊ฐ ๊ฐ๋ ์์ฑ์ผ๋ก, ํด๋์ค์์๋ static์ ๋ถ์ฌ ์ ์ ๋ฉ์๋๋ฅผ ์ ํ๋ค. class ์์ฒด๊ฐ ๊ฐ๋ ๋ฉ์๋๋ก ์ ๋๊ฐ์ด๋ ๋๋คํ ์๋ฅผ ์ป์ ๋ ์ฌ์ฉํ๋ Math.abs()๊ฐ static ํจ์์ ์๊ฐ ๋ ์ ์๋ค. ์ ์ ๋ฉ์๋๋ ์ธ์คํด์ค์๊ฒ ์์๋์ง ์๋ ๋ฉ์๋๋ก ๋ณ๋์ ์ธ์คํด์ค๋ฅผ ๋ง๋ค์ง ์๊ณ ์ฌ์ฉํด์ ์ ํธ๋ฆฌํฐ ํจ์๋ฅผ ๋ง๋ค ๋ ์ฌ์ฉ๋๋ค.
class Cat {
static shout = () => console.log("์ผ์น") // ์ ์ ๋ฉ์๋
}
Cat.shout() //์ผ์ฉ๐ฅ ์ธ์คํด์ค ์์ฑ๊ณผ์
ํด๋์ค์ ์ธ์คํด์ค๋ฅผ ๋ง๋๋ ๊ณผ์ ์ ์ ๋ฆฌํ๋ฉด ์์ฑ์ ํจ์์ ๋์ผํ๊ฒ ๋์ํ๋ค. ๊ฐ์ฅ ๋จผ์ new ํค์๋๋ก ๋น ๊ฐ์ฒด๋ฅผ ๋ง๋ค๊ณ this๋ฅผ ๋ฐ์ธ๋ฉํ๋ค. constructor ๋ด๋ถ์ฝ๋๋ฅผ ํตํด ์ด๊ธฐํํ ํ์ ์๋ต๋ return this๋ก ์ธ์คํด์ค๋ฅผ ๋ง๋ ๋ค. ํด๋์ค ๋ด๋ถ์ ์ธ์คํด์ค ๋ฉ์๋๋ ํ๋กํ ํ์
์ ํตํด ์์๋๊ธฐ ๋๋ฌธ์ ์์ฒด์ ์ผ๋ก๋ ๊ฐ์ง๊ณ ์์ง ์๋ค.
class Cat {
static shout = () => console.log("์ผ์น")
constructor(name) {
this.name = name
}
call() {
console.log("ํ์ด")
}
}
const cat = new Cat("์ผ์น")
console.log(cat.hasOwnProperty("call")) //false
console.log(cat.__proto__.hasOwnProperty("call")) //true๐ฉธ ํด๋์ค์ getter์ setter
ํด๋์ค๋ getter์ setter๋ฅผ ์ด์ฉํด ๋ฐ์ดํฐ ์์ฑ ๊ฐ์ ์ฝ๊ฑฐ๋ ๋ณ๊ฒฝํ ์ ์๋ค. getter์ setter๋ ๋ชจ๋ ํจ์์ง๋ง ์ฌ์ฉํ ๋๋ ๋ค๋ฅธ ์์ฑ๊ณผ ๋์ผํ๊ฒ ์ฌ์ฉํ๋ค. getter๋ ํด๋น ์์ฑ์ ์ ๊ทผํ ๋ ์ํ๋๋ ํจ์์ด๋ฉฐ ํญ์ ๊ฐ์ ๋ฐํํด์ค์ผ ํ๊ณ , setter๋ ํด๋น ์์ฑ์ ๋ณ๊ฒฝํ ๋ ์คํ๋๋ ํจ์์ด๋ฏ๋ก ํญ์ ์ธ์๊ฐ ํ์ํ๋ค. getter์ setter๊ฐ ํ์ํ ์ํฉ์ ๋ํด ์์๋ณด์.
class Person {
constructor(firstName, lastName) {
this.firstName = firstName
this.lastName = lastName
}
fullName() {
return `${this.firstName} ${this.lastName}`
}
}
const person1 = new Person(100, 90)
console.log(person1.averageScore()) //ํจ์๋ฅผ ์ด์ฉํด์ผํด์์ ์ฝ๋์ ์ ์์ ํ๊ท ๊ฐ์ ์ป๊ณ ์ถ์ ์ํฉ์์ ๋ฉ์๋๋ก ํ๊ท ์ ์๋ฅผ ๋ฐ์ ์ ์์ง๋ง averageScore๋ฅผ ์์ฑ์ผ๋ก ๋ง๋ค๊ณ ์ถ๋ค. ๊ทธ๋์ ์ฐ์ ์ ์ด๊ธฐ ๊ฐ์ผ๋ก ๋จผ์ ๋ฐ์์ฌ ๋ ๊ณ์ฐํด์ ์์ฑ์ผ๋ก ์ถ๊ฐํ ์ ์๋ค.
class Person {
constructor(firstName, lastName) {
this.firstName = firstName
this.lastName = lastName
this.fullName = `${this.firstName} ${this.lastName}`
}
}
const me = new Person("Youngjun", "Choi")
console.log(me.fullName) // Youngjun Choi
me.firstName = "hi"
console.log(me.fullName) // Youngjun Choiํ์ง๋ง ๋ฌธ์ ์ ์ ์ด๊ธฐํ๋ก ๊ฐ์ด ์ ํด์ ธ๋ฒ๋ ค ์ํ ์ ์๋ฅผ ์์ ํ์ ๋ ํ๊ท ๊ฐ์ ๋ฐ์์ด ์๋๊ณ ์๋ค. ์ด๋ ์ฌ์ฉํ ์ ์๋ ๊ฒ์ด Getter์ Setter๋ค.
class Person {
constructor(firstName, lastName) {
this.firstName = firstName
this.lastName = lastName
}
get fullName() {
return `${this.firstName} ${this.lastName}`
}
set fullName(value) {
this.fullName = value
}
}
const me = new Person("Youngjun", "Choi")
console.log(me.fullName)
me.fullName = "yj Choi" // RangeError: Maximum call stack size exceededgetter์ setter๋ ๋ด๋ถ์ ์ผ๋ก ํจ์์ด๊ธฐ ๋๋ฌธ ์์ฑ์ ์ ๊ทผํด ๊ฐ์ ๋ฐํํด์ฃผ๊ณ ๋ณ๊ฒฝํ ์ ์์ง๋ง ์ฌ์ฉ ์์๋ ์์ฑ์ผ๋ก ์ฌ์ฉํ ์ ์์ด ์ฐ๋ฆฌ๊ฐ ์ํ๋ ๊ฒฐ๊ณผ๋ฅผ ์ป์ ์ ์๋ค. ํ์ง๋ง ์ด๋ ์ฃผ์ํ ์ ์ setter๊ฐ ๋ณ๊ฒฝํ๋ ์์ฑ์ ์ด๋ฆ๊ณผ ์ ๊ทผํ๋ ์์ฑ์ ์ด๋ฆ์ด ๊ฐ์ ๊ฒฝ์ฐ ๊ณ์ํด์ ์ฌ๊ท์ ์ผ๋ก ํธ์ถํด ์๋ฌ๊ฐ ๋ฐ์ํ๋ค. ์ด๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด์๋ ๊ฐ์ setter ์์ฑ์ ์ง์ ๋ณ๊ฒฝํ๋ ๊ฒ์ด ์๋๋ผ ๋ด๋ถ ์์ฑ์ ์ด์ฉํด์ ์์ ํด์ผ ํ๋ค.
class Person {
constructor(firstName, lastName) {
this.firstName = firstName
this.lastName = lastName
}
get fullName() {
return `${this.firstName} ${this.lastName}`
}
set fullName(name) {
;[this.firstName, this.lastName] = name.split(" ")
}
}
const me = new Person("Youngjun", "Choi")
console.log(me.fullName) // Youngjun Choi
me.fullName = "yj Choi"
console.log(me.fullName) // yj Choigetter์ setter๋ ํด๋์ค ๋ ๋ฒจ์ ์ ๊ทผ์์ด๊ธฐ ๋๋ฌธ์ ํ๋กํ ํ์ ์ ์์ฑ์ด ๋๋ค.
๐บ ํด๋์ค์ ํ๋
ํด๋์ค์ ํ๋์ constructor ํจ์๋ก ์ด๊ธฐํํ์ง ์์๋ ๋๋ ์ธ์คํด์ค ์์ฑ์ ์ ์ํ ์ ์๋ค. ์ด๋ this๋ ์ฌ์ฉํด์๋ ์๋๊ณ ์ด๊ธฐ ๊ฐ์ด ์๋ค๋ฉด undefined๋ก ํ ๋น๋๋ค. ์ธ์คํด์ค ์์ฑ์ ํญ์ public์ด์ง๋ง ์ต์ ์๋ฐ์คํฌ๋ฆฝํธ๋ #์ผ๋ก private ํ๋, ํด๋์ค ๋ด๋ถ์์๋ง ์ฐธ์กฐ ๊ฐ๋ฅํ ์์ฑ์ ๋ง๋ค ์ ์๋ค.
class Person {
#name = "๋น๋ฐ"
get name() {
return this.#name
}
}
const me = new Person()
console.log(me.name)
console.log(me.#name) // SyntaxError: Private field '#name' must be declared in an enclosing classํด๋์ค ํ๋์ static์ ์ด์ฉํ๋ฉด ์์ ํด๋์ค ๋ ๋ฒจ์ ๋ฉ์๋๋ฅผ ๋ง๋ ๊ฒ์ฒ๋ผ ์์ฑ๋ ์ถ๊ฐํ ์ ์๋ค.
class Person {
#name = "๋น๋ฐ"
static male = "๋จ์"
get name() {
return this.#name
}
}
const me = new Person()
console.log(Person.male) // ๋จ์๐ ํด๋์ค์ ์์
ํ๋กํ ํ์
์ ์ ๋ฆฌํ๋ฉด์ ์๋ฐ์คํฌ๋ฆฝํธ๋ ํ๋กํ ํ์
์ ์ด์ฉํด ์์์ ์ง์ํ๋ค๊ณ ํ์๋ค. ํด๋์ค๋ ์์ฑ์ ํจ์๋ณด๋ค ํธํ๊ฒ extendsํค์๋๋ฅผ ์ด์ฉํด ์์์ ํ ์ ์๋ค. ๋ถ๋ชจ์ ์์ฑ๊ณผ ๋ฉ์๋๋ฅผ ์์์ ๋ฐ์ ์ฌ์ฉํ๊ธฐ ์ํด ํ์ํ superํค์๋ ์ ๋ํด ๋จผ์ ์์๋ณด์
Super
super๋ ๋ถ๋ชจ ํด๋์ค์ constructor๋ฅผ ํธ์ถํ๊ฑฐ๋, ๋ถ๋ชจ์ ๋ฉ์๋๋ฅผ ์ฐธ์กฐํ ๋ ์ฌ์ฉํ๋ค. ๊ฐ๊ฐ์ ๊ฒฝ์ฐ์ ๋ํด ์์๋ณด์.
๋จผ์ super๋ฅผ ํธ์ถํ ๋๋ ์ค์ํ ์ธ ๊ฐ์ง ๊ท์น์ด ์๋ค. ์ด๋ฌํ ๊ท์น์ ์์ ํด๋์ค๋ก ์ธ์คํด์ค๋ฅผ ๋ง๋ค๋ฉด์ ๋จผ์ ๋ถ๋ชจ ํด๋์ค์๊ฒ ์ธ์คํด์ค ์์ฑ์ ์์ํ๊ธฐ ๋๋ฌธ์ ์ง์ผ์ ธ์ผ ํ๋ค.
- ์์ ํด๋์ค์์ constructor๋ฅผ ํธ์ถํ๋ ๊ฒฝ์ฐ์๋ ๋ฐ๋์ super๋ฅผ ํธ์ถํด์ผ ํ๋ค.
class Parent {}
class Child extends Parent {
//ReferenceError: Must call super constructor in derived class before accessing 'this' or returning from derived constructor
constructor() {}
}
const child = new Child()- ์์ ํด๋์ค์ constructor์์ super๋ฅผ ํธ์ถํ๊ธฐ ์ ์ this๋ฅผ ์ฐธ์กฐํ ์ ์๋ค.
class Parent {}
class Child extends Parent {
constructor() {
//ReferenceError: Must call super constructor in derived class before accessing 'this' or returning from derived constructor
this.a = 1
super()
}
}
const child = new Child()- ์์์ constructor ํจ์์์๋ง super๊ฐ ํธ์ถ๋ ์ ์๋ค.
class Parent {}
class Child extends Parent {
constructor() {
this.a = 1
super()
}
foo() {
super() // SyntaxError: 'super' keyword unexpected here
}
}
const child = new Child()๋ ๋ฒ์งธ๋ก super๋ฅผ ์ด์ฉํด ๋ถ๋ชจ ํด๋์ค์ ๋ฉ์๋๋ฅผ ์ฐธ์กฐํ ์ ์๋ค.
class Parent {
constructor(name) {
this.name = name
}
sayHi() {
return `${this.name}`
}
}
class Child extends Parent {
sayHi() {
return `${super.sayHi()}` //parent.sayHi()
}
}
const child = new Child("yj")
console.log(child.sayHi())์ ์์ ๋ super๋ฅผ ํตํด Parent ํด๋์ค์ ํ๋กํ ํ์ ์ sayHi๋ฅผ ์ฐธ์กฐํ๋ค. ์ด๋ this๋ ์ธ์คํด์ค๋ฅผ ๊ฐ๋ฆฌํค๊ณ ์๊ธฐ ๋๋ฌธ์ name์ ์ฐธ์กฐํ ์ ์๋ค.
์ด๋ ๊ฒ super๋ฅผ ์ฐธ์กฐํ ์ ์๋ ๊ฒ์ ๋ฉ์๋๊ฐ ๋ด๋ถ ์ฌ๋กฏ [[HomeObject]]๋ฅผ ๊ฐ์ ธ, ๋ฐ์ธ๋ฉํ๊ณ ์๋ ๊ฐ์ฒด์ ํ๋กํ ํ์
์ ๊ฐ๋ฆฌํค๊ณ ์๋ค. sayHi()๋ฉ์๋์ [[HomeObject]]์๋ Child.prototype์ด ๋ฐ์ธ๋ฉ๋๊ณ super๋ฅผ ์ฐธ์กฐํด child.prototype์ ํ๋กํ ํ์
์ธ Parent.prototype์ ๊ฐ๋ฆฌํฌ ์ ์๋ค.
super๋ฅผ ์์ ํด๋์ค์ ์ ์ ๋ฉ์๋์์ ์ด์ฉํ๋ฉด ๋ถ๋ชจ์ ์ ์ ๋ฉ์๋๋ฅผ ์ฐธ์กฐํ ์ ์๋ค.
class Parent {
static sayHi() {
return `Hi `
}
}
class Child extends Parent {
static sayHi() {
return `${super.sayHi()}`
}
}
console.log(Child.sayHi()) // hi์ด์ ์ค์ ๋ก ์์์ ํตํด ๊ฐ์ฒด๋ฅผ ๋ง๋๋ ๊ณผ์ ์ ๋ํด ์์๋ณด์.
class Circle {
constructor(radius) {
this.radius = radius // ๋ฐ์ง๋ฆ
}
getPerimeter() {
return 2 * Math.PI * this.radius
}
getArea() {
return Math.PI * this.radius ** 2
}
}
// ์์ ํด๋์ค
class Cylinder extends Circle {
constructor(radius, height) {
super(radius)
this.height = height
}
getArea() {
return this.height * super.getPerimeter() + 2 * super.getArea()
}
getVolume() {
return super.getArea() * this.height
}
}
const cylinder = new Cylinder(2, 10)
console.log(cylinder.getPerimeter())
console.log(cylinder.getArea()) // 150.79644737231007
console.log(cylinder.getVolume()) // 125.66370614359172์์ ์์ ๋ก ์ธ์คํด์ค cylinder๊ฐ ๋ง๋ค์ด์ง๋ ๊ณผ์ ์ ์์๋๋ก ์ ๋ฆฌํ๋ฉด ๋ค์๊ณผ ๊ฐ๋ค.
-
Cylinder ํด๋์ค์ super ํธ์ถ
๋จผ์ Cylinder ํด๋์ค์ constructor๊ฐ ํธ์ถ๋๋๋ฐ ์ด๋ super๋ฅผ ํตํด Cylinder ํด๋์ค์์ Circleํด๋์ค๋ก ์ธ์คํด์ค ์์ฑ์ด ์์๋๋ค. ๊ทธ๋์ ์ค์ ๋ก ์ธ์คํด์ค๋ฅผ ๋ง๋๋ ๊ณณ์ Circle ํด๋์ค์ด๊ธฐ ๋๋ฌธ์ ์์ super๋ฅผ ๋จผ์ ํธ์ถํ์ง ์์ผ๋ฉด ์๋ฌ๊ฐ ๋ฐ์ํ๋ค.
- Circle ํด๋์ค์ ์ธ์คํด์ค ์์ฑ๊ณผ this ๋ฐ์ธ๋ฉ
superํธ์ถ๋ก Circleํด๋์ค๋ ์ธ์คํด์ค๋ฅผ ์์ฑํ๊ธฐ ์ํด ๋จผ์ {}๋น ๊ฐ์ฒด๋ฅผ ๋ง๋ค๊ณ this๋ฅผ ๋ฐ์ธ๋ฉํ๋ค. ์ด๋ this๋ ์ธ์คํด์ค๋ฅผ ๊ฐ๋ฆฌํค๊ณ ์ธ์คํด์ค์ ํ๋กํ ํ์
์ Circle.prototype์ด ์๋๋ผ Cylinder.prootype์ด ๋๋ค.
-
Circle ํด๋์ค์ ์ธ์คํด์ค ์ด๊ธฐํ
Circleํด๋์ค์ constructorํจ์๋ก ์ธ์คํด์ค๋ฅผ ์ด๊ธฐํํ ํ์ Cylinderํด๋์ค๋ก ๋ค์ ์ ๋ฌํ๋ค.
-
Cylinder ํด๋์ค์ this๋ฐ์ธ๋ฉ
Cylinderํด๋์ค์์๋ ๋ณ๋์ ์ธ์คํด์ค๋ฅผ ๋ง๋๋ ๊ฒ์ด ์๋๋ผ ์ ๋ฌ๋ฐ์ ์ธ์คํด์ค๋ฅผ this์ ๋ฐ์ธ๋ฉํ๋ค. ๊ทธ๋ ๊ธฐ ๋๋ฌธ์ super๋ฅผ ํธ์ถํ๊ธฐ ์ด์ ์ this๋ฅผ ์ฐธ์กฐํ ์ ์๋ค.
-
Cylinder ํด๋์ค์ ์ด๊ธฐํ
Cylinder ํด๋์ค์ constructorํจ์๋ก ์ธ์คํด์ค์ ์ด๊ธฐํ๋ฅผ ์งํํ ํ์ this๊ฐ ๋ฐํ๋๋ค.
์ด๋ฌํ ๊ณผ์ ์ ํตํด ๋ง๋ค์ด ์ง ์ธ์คํด์ค์ ํ๋กํ ํ์ ์ฒด์ธ์ ๋ค์ ๊ทธ๋ฆผ๊ณผ ๊ฐ์ด ํํ๋๋ค.
์ด๋ getArea ๋ฉ์๋๋ ํ๋กํ ํ์
์ฒด์ธ์์ Cylinder ํด๋์ค์ ์ํด override๋๋ฉด์ Circle.prototype ์ getArea๊ฐ ์๋ Cylinder.prototype์ getArea๋ก ํธ์ถ๋์ด ๊ฒฐ๊ณผ๊ฐ 4ฯ๊ฐ ์๋๋ผ 48ฯ์ ํด๋นํ๋ 150.79644737231007๋ก ๋ํ๋๋ค.
๋ง์น๋ฉฐ
์์ฑ์ ํจ์์ ํ๋กํ ํ์ ์ ์ดํดํ๊ณ ํด๋์ค๋ฅผ ๋ค์ ๋ณด๋ฉด์ ๊ณตํต์ ๊ณผ ์ฐจ์ด์ ์ ์๋กญ๊ฒ ์๊ฒ ๋์๋ค. ์ดํ์ ํ์ ์คํฌ๋ฆฝํธ์์ ์ข ๋ ๊ฐ๋ ฅํ ๊ฐ์ฒด ์งํฅ ์์๋ค์ ํจ๊ป ์ ๋ฆฌํ ์์ ์ด๋ค.
[์ฐธ์กฐ]