์ค๋์ ์ฃผ์
class
getter
setter
์์ !
๊ตฌ๊ธ๋งํด์ ๊ฐ์ ธ์จ ์์ ๊ฑฐ ์ฌ์ง์ ์์๋ก, Class ๋ฌธ๋ฒ์ ์ด์ฉํด ์์ ๊ฑฐ๋ผ๋ Instance(= ๊ฒฐ๊ณผ๋ฌผ)๋ฅผ ๋ง๋ค์ด๋ณด์.
โ
* Class๋? : ํ๋์ ์์ ๊ฑฐ๋ฅผ ๋ง๋ค๊ธฐ ์ํ '์ค๊ณ๋'.
โ
์์ ๊ฑฐ ์ค๊ณ๋๋ฅผ ๋๋ฉด์ด๋ผ๋ '์ข ์ด'์ ๊ทธ๋ฆฐ๋ค๋ฉด?
ํ๋ก๊ทธ๋จ์์๋ 'constructor'๋ผ๋ ์์ฑ์ ํจ์์ ๊ฐ๋ค์ ์ถ๊ฐํด๊ฐ๋ฉด์ ๋ง๋ฆ !
์ค๊ณ๋์ '์ข ์ด' = Constructor (= ์์ฑ์ ํจ์)
์ด์ ๊ทธ๋ผ constructor๋ก ์์ ๊ฑฐ๋ฅผ ๋ง๋ค์.
1. Class๋ก ๋ญ ๋ง๋ค์ง ์ง์ ํ๋ค.
class Bicycle {}
๋ ์์ ๊ฑฐ๋๊น Bicycle !!
2. {}์์ ์ด ์์ ๊ฑฐ๋ฅผ ๋ง๋ค๊ธฐ ์ํ ์ค๊ณ๋์ '์ข ์ด'์ญํ constructor(= ์์ฑ์ ํจ์) ๋ฅผ ๋ฃ์ด์ค๋ค.
class Bicycle {
constructor () {
}
}
์ด๋ฌ๋ฉด ์ค๋น ๋.
3. * ํ์์์(= Property)
์ด์ ๋๋ ์์ ๊ฑฐ๋ฅผ ๋ง๋ค์ค๋น๊ฐ ๋๋ฌ๋๋ฐ ์์ ๊ฑฐ์ ๋ญ๊ฐ ํ์์ ์ผ๋ก ํ์ํ์ง, 'ํ์์์'๋ฅผ ์๊ฐํด๋ณด์ !!
์ผ๋จ, ๊ตด๋ฌ๊ฐ๋ ค๋ฉด '๋ฐํด'์ 'ํ๋ฌ' ๊ทธ๋ฆฌ๊ณ , ์์ง์์ ์กฐ์ข ํ๋ ค๋ฉด 'ํธ๋ค' ์ด๋๊น, ์ผ๋จ ์ด ์ ๋๋ก๋ง ๋ง๋ค์ด๋ณด์
1.๋ฐํด / 2.ํ๋ฌ / 3.ํธ๋ค
class Bicycle {
constructor (tire, pedal, handle) {
}
}
์์ฑ์ ํจ์์ ํ์์์๋ ๋ณด๊ธฐ์ ๊ฐ์ด ()์์ ,๋ฅผ ์ด์ฉํด์ ๊ตฌ๋ถํด์ ๋ฃ์ด์ค๋ค.
class Bicycle {
constructor(tire, pedal, handle) {
this.tire = tire;
this.pedal = pedal;
this.handle = handle;
}
}
๊ทธ๋ฆฌ๊ณ ๋ค์ด์ค๋ ํ์์์๋ค์ ๊ฐ์ ๋ค๋ฅธ ์์ ๊ฑฐ์ ๋ง๊ฒ ๋ฃ์ด์ฃผ๊ธฐ ์ํด
'this' ๋ฅผ ์ด์ฉํด์ ๊ตฌ๋ถ์ ํด์ค๋ค.
์ด๋ฌ๋ฉด ํ์์์ ๋.
* this๋ฅผ ๋ฌ์์ฃผ๋ ์ด์ : this๋ j.s(์๋ฐ์คํฌ๋ฆฝํธ)์์์ ๊ต~~์ฅํ ๋ค์ํ ์๋ฏธ์ ์ฉ๋๋ก ํ์ฉ๋๋๊ฒ ๊ฐ๋ค.
๋๋ ์์ง ๋ชจ๋ ๋ด์ฉ์ ์๊ณ ์์ง ๋ชปํ์ง๋ง,
์ฌ๊ธฐ์์ this๋ ์์ Bicycle์ด๋ผ๋ Instance(๋ด๊ฐ ๋ง๋ค์ด๋ผ ๊ฒฐ๊ณผ๋ฌผ)์ ์ง์นญํ๋ค.
class Bicycle {
constructor(tire, pedal, handle) {
this.tire = tire;
this.pedal = pedal;
this.handle = handle;
}
}
let greenBicycle = new Bicycle("๊ธํธํ์ด์ด", "ํฉ๊ธ๋นํ๋ฌ", "๊ฐ๋ฒผ์ดํธ๋ค");
let redBicycle = new Bicycle("๋ฏธ์๋ฆฐํ์ด์ด", "๊ฒ์์ดํ๋ฌ", "๋ฌ๋ธํธ๋ค");
let blueBicycle = new Bicycle("ํ๊ตญํ์ด์ด", "๋ฌํฝ์ดํ๋ฌ", "ํ์ธํธ๋ค");
console.log("์ด๊ฑด ์ด๋ก ์์ ๊ฑฐ -> ", greenBicycle);
console.log("์ด๊ฑด ๋นจ๊ฐ ์์ ๊ฑฐ -> ", redBicycle);
console.log("์ด๊ฑด ํ๋ ์์ ๊ฑฐ -> ", blueBicycle);
* this ์ค๋ช :
์ด๋ก ์์ ๊ฑฐ๋ฅผ ๋ณด์,
"๊ธํธํ์ด์ด" ๋ผ๋ ์ด๋ก์์ ๊ฑฐ์ ํ์์์๋,
์ ์ฝ๋์ 'tire' ๋ถ๋ถ์ ๋ค์ด๊ฐ์,
this.tire๋ก ์ง์ ๋๊ณ ,
this๋ ์ฌ๊ธฐ์ ๋ด๊ฐ ๋ง๋ค ๊ฒฐ๊ณผ๋ฌผ์ธ Instance๋ฅผ ์ง์นญํ๊ณ ์์ผ๋ !!
์ ๊ฑด Class๋ผ๋ ์ค๊ณ๋๋ฅผ ์ฌ์ฉํด์ ์๋กญ๊ฒ ๋ง๋ (=new Bicycle) ์ด๋ก ์์ ๊ฑฐ์ tire๋ค.
โ
์ด๋ฐ์์ผ๋ก ๋ฏธ์๋ฆฐ์ ๋ ๋ค๋ฅธ new Bicycle tire๊ฐ ๋๊ณ ,
ํ๊ตญํ์ด์ด๋ ๋ง์ฐฌ๊ฐ์ง.
โ
์ด๋ ๊ฒ this๋ฅผ ์ฌ์ฉํด์ ์ง๊ธ ๋ค์ด์จ ํ์ด์ด๊ฐ ์ด๋ค ๋์์ ํ์ด์ด์ธ์ง ์ง์นญ์ ํด์ฃผ์ด์ผ,
์๋ก ๋ค๋ฅธ ํ์์์๋ค์ ๊ฐ์ง ์๋ก์ด ๊ฐ์ฒด๋ค์ ๋ง๋ค ์ ์๋ค.
์ด๋ ๊ฒ console.log()๋ก ๊ฐ๊ฐ์ ์์ ๊ฑฐ๊ฐ ์ ๋ง๋ค์ด์ง๋์ง ์ฐ์ด๋ณด๋ฉด,
์๋ก์ ์์ ๊ฑฐ๊ฐ ๊ฐ์์ ํ์์์๋ค๋ก ์ ๋ง๋ค์ด์ง๊ฑธ ํ์ธํ ์ ์๋ค.
4. ๋ฉ์๋ (= method)
๊ทธ๋ฆผ์ ๋ณด๋ฉด ๋ฉ์๋์ start() , break() , stop()์ด ์๋๊ฑธ ์ ์ ์๋ค.
๋๋ ์ฌ๊ธฐ์ break ๊ธฐ๋ฅ์ด ์ ์ผ ์๊ธํด ๋ณด์ด๋ ์์ ๊ฑฐ์ break๋ฅผ ๋ฌ์์ฃผ๊ฒ ๋ค.
class Bicycle {
constructor(tire, pedal, handle) {
this.tire = tire;
this.pedal = pedal;
this.handle = handle;
}
break() {
console.log("์๋๊ฐ ๊ฐ์ํ๊ณ ์์ต๋๋ค.");
}
}
break() ๋ผ๋ ๋ฉ์๋๋ฅผ ์ฐ๋ฆฌ์ ์ค๊ณ๋ class ์์ ๋ฃ์ด์ฃผ๋ฉด์, ์์ ๊ฑฐ๊ฐ ๋ง๋ค์ด์ง๋ฉด ์๋์ผ๋ก ๊ทธ ์์ ๊ฑฐ๋
break() ๋ฉ์๋ ๋ผ๋ ์์ฑ์ ๊ฐ๊ณ ๋ง๋ค์ด ์ง๋ ๊ฒ์ด๋ค.
๋ธ๋ ์ดํฌ ๊ธฐ๋ฅ์ด ์ ๋ง๋ค์ด ์ก๋์ง break๋ฅผ ์คํ(= () )์์ผ์ ํ์ธํด๋ณด์.
class Bicycle {
constructor(tire, pedal, handle) {
this.tire = tire;
this.pedal = pedal;
this.handle = handle;
}
break() {
console.log("์๋๊ฐ ๊ฐ์ํ๊ณ ์์ต๋๋ค.");
}
}
let greenBicycle = new Bicycle("๊ธํธํ์ด์ด", "ํฉ๊ธ๋นํ๋ฌ", "๊ฐ๋ฒผ์ดํธ๋ค");
let redBicycle = new Bicycle("๋ฏธ์๋ฆฐํ์ด์ด", "๊ฒ์์ดํ๋ฌ", "๋ฌ๋ธํธ๋ค");
let blueBicycle = new Bicycle("ํ๊ตญํ์ด์ด", "๋ฌํฝ์ดํ๋ฌ", "ํ์ธํธ๋ค");
// console.log("์ด๊ฑด ์ด๋ก ์์ ๊ฑฐ -> ", greenBicycle);
// console.log("์ด๊ฑด ๋นจ๊ฐ ์์ ๊ฑฐ -> ", redBicycle);
// console.log("์ด๊ฑด ํ๋ ์์ ๊ฑฐ -> ", blueBicycle);
greenBicycle.break();
์ ๋ง๋ค์ด ์ก๋ค๋ฉด, "์๋๊ฐ ๊ฐ์ํ๊ณ ์์ต๋๋ค"๋ผ๋ ๋ฉ์์ง๊ฐ ์ถ๋ ฅ์ด ๋ ๊ฒ์ด๋ค.
์ ์คํ์ด ๋๋๊ฒ์ ํ์ธํ๋ค.
this๋ฅผ ์ด์ฉํด์ ์ด๋ฐ๊ฒ๋ ํ ์ ์๋ค.
โ
break๊ฐ ์คํ = () ๋ ๋์ ๋ด์ฉ์ ์์ ๊ฑฐ๋ง๋ค ๋ค๋ฅด๊ฒ ํํํด์ฃผ๊ณ ์ถ๋ค๋ฉด,
break() {
console.log(this.pedal + " ๋๊ฐ์ ๋ค !! ๊ธธ์ ๋น์ผ๋ผ !!");
}
์ด๋ ๊ฒ ๋ง๋ค์ด์ค๋ค๋ฉด, ๊ฐ์์ ํ๋ฌ์ด๋ฆ์ ์ธ์น๋ฉด์ ๋ค์ ๋ฌธ์์ด๊ณผ ํจ๊ป ์ถ๋ ฅ์ด ๋ ๊ฒ์ด๋ค.
class Bicycle {
constructor(tire, pedal, handle) {
this.tire = tire;
this.pedal = pedal;
this.handle = handle;
}
break() {
console.log(this.pedal + " ๋๊ฐ์ ๋ค !! ๊ธธ์ ๋น์ผ๋ผ !!");
}
}
let greenBicycle = new Bicycle("๊ธํธํ์ด์ด", "ํฉ๊ธ๋นํ๋ฌ", "๊ฐ๋ฒผ์ดํธ๋ค");
let redBicycle = new Bicycle("๋ฏธ์๋ฆฐํ์ด์ด", "๊ฒ์์ดํ๋ฌ", "๋ฌ๋ธํธ๋ค");
let blueBicycle = new Bicycle("ํ๊ตญํ์ด์ด", "๋ฌํฝ์ดํ๋ฌ", "ํ์ธํธ๋ค");
// console.log("์ด๊ฑด ์ด๋ก ์์ ๊ฑฐ -> ", greenBicycle);
// console.log("์ด๊ฑด ๋นจ๊ฐ ์์ ๊ฑฐ -> ", redBicycle);
// console.log("์ด๊ฑด ํ๋ ์์ ๊ฑฐ -> ", blueBicycle);
greenBicycle.break();
redBicycle.break();
blueBicycle.break();
์ด๋ฐ์์ผ๋ก ๋ง์ด๋ค.
* (``)๋ฐฑํฑ์ ์ด์ฉํ๋ ๋ฒ
break ๋ฉ์๋์ ์ฝ๋๋ฅผ ``์ ์ด์ฉํด์ ๋ค๋ฅด๊ฒ ๋ฐ๊ฟ๋ณธ๋ค.
break() {
console.log(`${this.pedal} ์ผ๋ผ๋ผ๋ 112351 +_+)#$%+#$35`);
}
์ด๋ฐ์์ผ๋ก ``์ ์ด์ฉํ๋ฉด ์์ ์ ๋งํ ๋ชจ๋ ๋ด์ฉ๋ค์ ๋ด์ ์ ์๋ค.
* ๊ทธ๋ฆฌ๊ณ `` ์์์ ๋์์ ์ง์นญํ ๋๋ ${}๋ฅผ ์จ์ฃผ๊ณ ์ค๊ดํธ ์์ ์ง์นญํ ๋์์ ๋ฃ์ด์ฃผ๋ฉด ๋๋ค.
์ถ๊ฐ ์ฝ๋ ํด์
let greenBicycle = new Bicycle("๊ธํธํ์ด์ด", "ํฉ๊ธ๋นํ๋ฌ", "๊ฐ๋ฒผ์ดํธ๋ค");
์ greenBicycle์ ํด์ํ๋ฉด, class Bicycle์ด๋ผ๋ '์ค๊ณ๋' ์ ๊ทผ๊ฑฐํด์,
ํ์ด์ด๋ ๊ธํธํ์ด์ด๊ณ , ํ๋ฌ์ ํฉ๊ธ๋นํ๋ฌ์ด๊ณ , ํธ๋ค์ ๊ฐ๋ฒผ์ด ํธ๋ค์ธ~
์๋ก์ด ์์ ๊ฑฐ๋ฅผ ํ๋ ๋ง๋ค์ด ์ค !! ๋ผ๋ ๋ป์ผ๋ก ์ดํด๊ฐ ๊ฐ๋ฅํ๋ค.
getter ์ setter
โ
์ด getter ์ setter ๋ผ๋ ๊ฐ๋ ์ด ์๋ ์ด์ !
โ
1. ์ฐ๋ฆฌ๊ฐ class๋ผ๋ ์ค๊ณ๋๋ฅผ ํตํด์ ๊ฒฐ๊ณผ๋ฌผ (= instance)๋ฅผ ๋ง๋ ๋ค.
2. ๊ทธ ์์๋ ๊ฒฐ๊ณผ๋ฌผ์ ๋ง๋ค๊ธฐ ์ํ ํ์์์๋ค (= Properties) ์ด ์๋ค.
3. ํ์ง๋ง, ๊ทธ ํ์์์๋ค์ ๊ฐ์ด ์ณ์ง ๋ชปํ ์์๋ค๋ก ์ด๋ฃจ์ด์ ธ ์๋ค๋ฉด ??
4. ์ฐ๋ฆฌ๋ ๊ทธ ํ๋กํผํฐ์ ์ ๊ทผํ๊ณ ๊ฒ์ฆ์ ํด ์ค ํ์๊ฐ ์๋ค.
Ex) pedal์ด๋ผ๋ ํ์์์์ ์ซ์๋ฅผ ๋ฃ์ด์ค๋ค๋์ง, handle์๋ ๊ฐ์ ๋ฃ์ง ์๋๋ค๋์ง ๋ฑ๋ฑ
โ
๊ทธ๋์ ๊ฐ์ ๊ฐ์ ธ์ค๋ ์ ๊ทผ ๋ฐฉ์์ธ 'getter'์,
๊ฐ์ ์ธํ ํด์ฃผ๋ 'setter'๊ฐ ์๋ค !
โ
์ธํ ์ ์๋์ ๊ฐ์ด ํด์ค๋ค.
class Bicycle {
constructor(tire, pedal, handle) {
this._tire = tire;
this._pedal = pedal;
this._handle = handle;
}
get tire() {
return this._tire;
}
set tire(value) {
if (typeof value === "number") {
console.log(`๋ฌธ์๋ฅผ ์
๋ ฅํด์ฃผ์ธ์ !`);
return;
} else if (value.length <= 1) {
console.log(`์ ๋ณด๋ฅผ ์
๋ ฅํด์ฃผ์ธ์ !`);
return;
}
this._tire = value;
}
}
let redBicycle = new Bicycle("๋ฏธ์๋ฆฐํ์ด์ด", "๊ฒ์์ดํ๋ฌ", "๋ฌ๋ธํธ๋ค");
console.log("์ด๊ฑด ์ด๋ก ์์ ๊ฑฐ -> ", redBicycle);
redBicycle.tire = 5;
console.log("์ด๊ฑด ์ด๋ก ์์ ๊ฑฐ -> ", redBicycle);
get์ ๊ฐ์ ๋ฐํํด์ฃผ๋ ์น๊ตฌ์ด๊ธฐ ๋๋ฌธ์, ํญ์ return๋ฌธ์ด ๋ค์ด๊ฐ๋ค !!
set์ ๋ค์ด๊ฐ๋ ๊ฐ์ ๊ฒ์ฆ์ ํ์๋ก ํ ๋, ์ฃผ๋ก ์ฌ์ฉํ๋ค.
โ
set์ ๋ค์ด์ค๋ ๊ฐ์ด value๊ธฐ ๋๋ฌธ์ ๋งค๊ฐ๋ณ์ ์๋ฆฌ์ ํต์์ ์ผ๋ก value๋ฅผ ๋ฃ์ด์ค๋ค.
์ ํจ์์ set ๋ถ๋ถ์ ํด์ํด๋ณด์๋ฉด
๋ค์ด์จ value์ type์ด number ๋ผ๋ฉด '๋ฌธ์๋ฅผ ์ ๋ ฅํด์ฃผ์ธ์'๋ฅผ ์ฐ๊ณ return์ํจ๋ค.
์ด๋ฐ์์ผ๋ก ๊ฐ๊ฐ์ ํ์์์์ ๋ค์ด์ค๋ ๊ฐ์ ์ค๋ฅ๋ฅผ ๊ฒ์ฆํ ์ ์๋ค.
์ด๋ ๊ฒ setter์ ๊ฑธ๋ ค์ ๊ฐ์ด ๋ค์ return ๋์ด ์ถ๋ ฅ๋๋๊ฒ์ ํ์ธํ ์ ์๋ค.
RangeError: Maximum call stack size exceeded => ์ค๋์ ์ค๋ฅ !!!!
PC@DESKTOP-TI9SBJL MINGW64 ~/Desktop/sparta/Week_5
$ node 06.js
C:\Users\PC\Desktop\sparta\Week_5\06.js:20
this.tire = value;
^
RangeError: Maximum call stack size exceeded
at set tire [as tire] ?[90m(C:\Users\PC\Desktop\sparta\Week_5\?[39m06.js:20:15?[90m)?[39m
at set tire [as tire] ?[90m(C:\Users\PC\Desktop\sparta\Week_5\?[39m06.js:20:15?[90m)?[39m
at set tire [as tire] ?[90m(C:\Users\PC\Desktop\sparta\Week_5\?[39m06.js:20:15?[90m)?[39m
at set tire [as tire] ?[90m(C:\Users\PC\Desktop\sparta\Week_5\?[39m06.js:20:15?[90m)?[39m
at set tire [as tire] ?[90m(C:\Users\PC\Desktop\sparta\Week_5\?[39m06.js:20:15?[90m)?[39m
at set tire [as tire] ?[90m(C:\Users\PC\Desktop\sparta\Week_5\?[39m06.js:20:15?[90m)?[39m
at set tire [as tire] ?[90m(C:\Users\PC\Desktop\sparta\Week_5\?[39m06.js:20:15?[90m)?[39m
at set tire [as tire] ?[90m(C:\Users\PC\Desktop\sparta\Week_5\?[39m06.js:20:15?[90m)?[39m
at set tire [as tire] ?[90m(C:\Users\PC\Desktop\sparta\Week_5\?[39m06.js:20:15?[90m)?[39m
at set tire [as tire] ?[90m(C:\Users\PC\Desktop\sparta\Week_5\?[39m06.js:20:15?[90m)?[39m
Node.js v18.16.0
getter ์ setter๋ฅผ ์ธ ๋, ํญ์ ์ฃผ์ํด์ผ ํ ๋ถ๋ถ์ด ๋ฐ๋ก ์ด ์ค๋ฅ์ด๋ค.
์ค๋ฅ ๋ฐ์์ง์ : 06.js ํ์ผ์ 20๋ฒ ์งธ ์ค
โ
์ค๋ฅ ๋ฌธ๊ตฌ : this.tire = value;
์๋ชป๋ ์ ์ด ๋ญ๊น ?
โ
RangeError: Maximum call stack size exceeded ์ด ์ค๋ฅ๋ฅผ ํด์ํ์๋ฉด ๋ค์๊ณผ ๊ฐ๋ค.
์ฝ์คํ์ ์์ผ ์ ์๋, ๊ฐ์ ์ฒ๋ฆฌํ ์ ์๋ ์ฌ์ด์ฆ, ๋ฒ์๋ฅผ ์ด๊ณผํ๋ค. ๊ธฐ์ค์น์ ํ๋๋ฅผ ์ด๊ณผํ๋ค.
ํ ๋ง๋๋ก '๋ฌดํ๋ฃจํ'์ ๋น ์ก๋ค๊ณ ์ ์ํ๋ค.
โ
์ ์ค๋ฅ ๋์ค๋ฉด === ๋ฌดํ๋ฃจํ
๊ทธ๋ผ ์ด์ ๋ ?
set tire(value) {
if (typeof value === "number") {
console.log(`๋ฌธ์๋ฅผ ์
๋ ฅํด์ฃผ์ธ์ !`);
return;
} else if (value.length <= 1) {
console.log(`์ ๋ณด๋ฅผ ์
๋ ฅํด์ฃผ์ธ์ !`);
return;
}
this.tire = value;
}
// ์ฐ๋ฆฌ๊ฐ Class Bicycle๋ก ์๋ก์ด ์์ ๊ฑฐ๋ฅผ ๋ง๋ค๊ธฐ ์ํด์
// let redBicycle = new Bicycle("๋ฏธ์๋ฆฐํ์ด์ด", "๊ฒ์์ดํ๋ฌ", "๋ฌ๋ธํธ๋ค");
// ์ด๋ ๊ฒ ๊ฐ๊ฐ์ tire, pedal, handle์ ๊ฐ์ ๋ถ์ฌํ๊ณ ,
// ์์ฑ์ ํจ์์ธ constructor์ ํด๋น ์์์ ๊ฐ๋ค์ ๋ฃ์ด์ฃผ๊ธฐ ์ ์~
// setter ์ญํ ์ ํด์ฃผ๋ ํจ์๊ฐ ์๋ค๋ฉด ??
// ์์ ๋ณด์ด๋ ํจ์๋ก ๋ค์ด๊ฐ์ ๊ฒ์ฆ๊ณผ์ ์ ๊ฑฐ์น๊ฒ ๋๋ค.
// ๋ค์ด์ค๋ ๊ฐ์ด number ํํ๋ ์๋์ง ~ ๋ค์ด์จ value์ ๋ฌธ์๊ธธ์ด๊ฐ 1๋ณด๋ค ์๊ฑฐ๋ ๊ฐ์ง ์์์ง
// ๊ฒ์ฆ์ ๊ฑฐ์น๊ณ ๋ค์ด์จ value๊ฐ ์ ์์ด๋ผ๋ฉด, ์ ์์ด๋ผ๋ ๊ฒฐ๊ณผ๊ฐ์ this.tire = value;(= ๊ฒ์ฆ์ ๋ง์น ๊ฐ) ์ ์ํด ์ฃผ๋๋ฐ,
// ์ด๋ ๊ฒ ์ง์ ์ด ๋๋ฉด ์ฐ๋ฆฌ์ this๋ instance์ธ new Bicycle์ ๊ฐ๋ฆฌํค๊ธฐ ๋๋ฌธ์,
// ๋ ๋ค์ ์
๋ ฅ๋ tire๊ฐ์ ๊ฒ์ฆ์ ๊ฑฐ์น๋ฌ ์์ ๊ฐ์ ๊ณผ์ ์ ๋ฐ๋ณตํ๊ฒ ๋๋ค.
// ๊ทธ๋ ๊ฒ 1๋ฒ,2๋ฒ,3๋ฒ,4๋ฒ .......... n๋ฒ '๋ฌดํ๋ฃจํ'์ ๋น ์ง๊ฒ ๋๊ณ ,
// ๊ทธ๋ ๊ธฐ ๋๋ฌธ์ call stack ์๋ ์ฒ๋ฆฌํด์ผ ํ๋ ์ ๋ณด๋ค์ด ๊ณ์ํด์ ์์ด๊ฒ ๋๋ฉด์
// ์ต๋์น, ํ๊ณ ๋ฒ์๋ฅผ ์ด๊ณผํ๊ฒ ๋๋ ๊ฒ์ด๋ค.
// ๊ทธ๋ฌ๋ฉด์ ๋์ค๊ฒ ๋ Error ๋ฉ์์ง๊ฐ
RangeError: Maximum call stack size exceeded
// ์ด๋ค.
RangeError: Maximum call stack size exceeded ์ค๋ฅ ํด๊ฒฐ ๋ฐฉ๋ฒ
โ
this์ ์ ๊ทผํ๋ ๋ชจ๋ property(= ํ์์์)์ ์ด๋ฆ์์๋ ํญ์ _(์ธ๋์ค์ฝ์ด)๋ฅผ ๋ถ์ธ๋ค.
โ
์ด์ :
โ
_(์ธ๋์ค์ฝ์ด) ๋ ํ๋ก๊ทธ๋จ์ ์ธ๊ณ์์ private๋ผ๋ ์๋ฏธ๋ฅผ ๋ง์ด ๊ฐ์ง๋ค๊ณ ํ๋ค.
๋ป์ ์๋ฐํ๊ณ , ๋ฌด์ธ๊ฐ๋ฅผ ๊ฐ ์ถ ๋~! ๊ทธ๋ด ๋ _๋ฅผ ํตํด์ ๋ถ๋ฆฌ๋ฅผ ์์ผ์ค ์ ์๋ค.
โ
this๊ฐ ์ฐ๋ฆฌ๊ฐ ์๋ก๋ง๋ค instance ๋ด์์๋ง ์๋ฏธ๋ฅผ ๊ฐ์ง ์ ์๊ฒ _(์ธ๋์ค์ฝ์ด)๋ฅผ ํตํด์ ๋ถ๋ฆฌ๋ฅผ ํด์ค์ผ ํ๋ค.
๊ทธ๋์ผ ์ง๊ธ๊ณผ ๊ฐ์ ์ค๋ฅ๊ฐ ํด๊ฒฐ์ด ๋๋ค.
(this ์ ์ ๊ทผํ๋ ๋ชจ๋ properties ์์๋ ์ ๋ถ _๋ฅผ ๋ถ์ฌ์ค์ผ ํ๋ค.)
์ค๋์ ์์ฑ ์ฝ๋.
class Bicycle {
constructor(tire, pedal, handle) {
this._tire = tire;
this._pedal = pedal;
this._handle = handle;
}
get tire() {
return this._tire;
}
set tire(value) {
if (typeof value === "number") {
console.log(`๋ฌธ์๋ฅผ ์
๋ ฅํด์ฃผ์ธ์ !`);
return;
} else if (value.length <= 1) {
console.log(`์ ๋ณด๋ฅผ ์
๋ ฅํด์ฃผ์ธ์ !`);
return;
}
this._tire = value;
}
break() {
console.log(`${this.pedal} ์ผ๋ผ๋ผ๋ 112351 +_+)#$%+#$35`);
}
// break() {
// console.log(this.pedal + " ๋๊ฐ์ ๋ค !! ๊ธธ์ ๋น์ผ๋ผ !!");
// }
}
let greenBicycle = new Bicycle("๊ธํธํ์ด์ด", "ํฉ๊ธ๋นํ๋ฌ", "๊ฐ๋ฒผ์ดํธ๋ค");
let redBicycle = new Bicycle("๋ฏธ์๋ฆฐํ์ด์ด", "๊ฒ์์ดํ๋ฌ", "๋ฌ๋ธํธ๋ค");
let blueBicycle = new Bicycle("ํ๊ตญํ์ด์ด", "๋ฌํฝ์ดํ๋ฌ", "ํ์ธํธ๋ค");
console.log("์ด๊ฑด ์ด๋ก ์์ ๊ฑฐ -> ", greenBicycle);
console.log("์ด๊ฑด ๋นจ๊ฐ ์์ ๊ฑฐ -> ", redBicycle);
console.log("์ด๊ฑด ํ๋ ์์ ๊ฑฐ -> ", blueBicycle);
greenBicycle.break();
redBicycle.break();
blueBicycle.break();
redBicycle.tire = "์ธ์์ ํ๋";
console.log("์ด๊ฑด ๋นจ๊ฐ ์์ ๊ฑฐ -> ", redBicycle);