๐ฆจ class
ํด๋์ค๋ ๊ฐ์ฒด์งํฅ ํ๋ก๊ทธ๋๋ฐ์ ํต์ฌ์ ๋๋ค.
๊ฐ์ฒด์งํฅ ํ๋ก๊ทธ๋๋ฐ์ด๋, ํ๋ก๊ทธ๋จ์ ๊ฐ์ฒด๋ค๋ก ๊ตฌ์ฑํ๊ณ ,
๊ฐ์ฒด๋ค ๊ฐ์ ์๋ก ์ํธ์์ฉํ๋๋ก ์์ฑํ๋ ๋ฐฉ๋ฒ์ ๋๋ค.
ํด๋์ค๋ { num: 1 } ์ฒ๋ผ ์๊ธด ๊ฐ์ฒด(object)๋ฅผ ์ ์ค๊ณํ๊ธฐ ์ํ ํ์ ๋ง์ต๋๋ค.
๊ทธ๋ฐ๋ฐ ์ด ๋์ ๊ฐ์ฒด๋ ํน์ ๋ก์ง์ ๊ฐ๊ณ ์๋ ํ๋(method)์ ๋ณ๊ฒฝ ๊ฐ๋ฅํ ์ํ(๋ฉค๋ฒ ๋ณ์)๋ฅผ ๊ฐ์ง๋๋ค.
์ํ๋ ๊ตฌ์กฐ์ ๊ฐ์ฒด ํ์ ์ง๋๊ณ , ๋น์ทํ ๋ชจ์์ ๊ฐ์ฒด๋ฅผ ๊ณต์ฅ์ฒ๋ผ ์ฐ์ด๋ผ ์ ์์ต๋๋ค.
๊ฐ์ฒด๋ฅผ ๋งค๋ฒ ๋ง๋ค์ด์ ์ฌ์ฉํด๋ ์ข์ง๋ง,
ํฐ ํ๋ชจ์ ๊ฐ์ฒด์ด๊ฑฐ๋ ๋น์ทํ ๋ชจ์์ ๊ฐ์ฒด๋ฅผ ๊ณ์ ๋ง๋ค์ด์ผ ํ๋ค๋ฉด,
class๋ผ๋ ์ค๊ณ๋๋ฅผ ํตํด ๋ง๋ค ์ ์์ต๋๋ค.๋จผ์ , 'ray' ์ฐจ๋ฅผ ๊ฐ์ฒด๋ก ์ ์ํด๋ณด๊ฒ ์ต๋๋ค.
ray ๋ผ๋ ๊ฐ์ฒด๋ 5๊ฐ์ ํ๋กํผํฐ๋ฅผ ๊ฐ๊ณ ์์ต๋๋ค.
๊ฐ ํ๋กํผํฐ ์ด๋ฆ์ ์๋์ ๊ฐ์ต๋๋ค.
- name
- price
- getName
- getPrice
- applyDiscount
let ray = { name: 'Ray', price: 2000000, getName: function() { // ray ๋ผ๋ ๊ฐ์ฒด์ getName ํ๋กํผํฐ ๊ฐ์ ํจ์๊ฐ ํ ๋น๋จ. return this.name; }, getPrice: function() { // getPrice ํ๋กํผํฐ ๊ฐ ์์ ํจ์๊ฐ ํ ๋น๋จ. return this.price; }, applyDiscount: function(discount) { // applyDiscount ํ๋กํผํฐ ๊ฐ ์์ ํจ์๊ฐ ํ ๋น๋จ. return this.price * discount; } }
ํ๋กํผํฐ ๊ฐ์ ํจ์๊ฐ ํ ๋น๋์์ต๋๋ค.
์ด๋ ๊ฒ ๊ฐ์ฒด์ ํ๋กํผํฐ ๊ฐ์๋ ํจ์๋ ๋ฃ์ ์ ์์ต๋๋ค.์์์ getPrice๋ผ๋ ํจ์๋ ๋ค์๊ณผ ๊ฐ์ด ํธ์ถํ ์ ์์ต๋๋ค.
const rayPriceByFunction = ray.getPrice(); console.log('ํจ์๋ก ์ ๊ทผ => ' +rayPriceByFunction);
๊ฐ์ฒด ๋ด๋ถ์์, ํด๋น ๐งโโ๏ธ๊ฐ์ฒด์ ํ๋กํผํฐ์ ์ ๊ทผํ๋ ค๋ฉด
"this"
๋ผ๋ ํค์๋๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค.
๊ทธ๋์ getPrice ๋ฉ์๋์์ this.price๋ก price ํค์ ์ ๊ทผํ ์ ์์๊ณ ,
2000000 ๊ฐ์ ๊ฐ๊ณ ์ฌ ์ ์์ต๋๋ค.
์๋ก์ด ๋ชจ๋ธ์ด ๋์๋ค๋ฉด, ์ฌ๋ฌ ์ฝ๋๋ฅผ ๋ค์ ์ธ ํ์์์ด ํ์ํ ์ ๋ณด๋ฅผ ๋ด์ Car๋ผ๋ ํด๋์ค(class)๋ฅผ ์์ฑํ์ฌ ๊ด๋ฆฌํ ์ ์์ต๋๋ค.class Car { constructor(name, price) { this.name = name; this.price = price; this.department = "์ ๋ฆ์ง์ "; this.salesAmount = 0; } applyDiscount(discount) { return this.price * discount; } addSales() { this.salesAmount++; } }
๐ฆจ ์์ฑ์(Constructor)
๊ฐ์ฒด(object)์ ์ค๊ณ๋์ธ ํด๋์ค(class)๋ ๋ฌธ๋ฒ์ด ๋น์ทํฉ๋๋ค.
๋์ ๊ฐ์ฅ ํฐ ์ฐจ์ด๋ cunstructor ๋ผ๋ ์์ฑ์ ํจ์์ ๋๋ค.
์๋์ ๊ฐ์ด class๋ก ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ๊ณผ์ ์ '์ธ์คํด์คํ'๋ผ๊ณ ๋ถ๋ฆ ๋๋ค.const morning = new Car('Morning', 2000000);
class๋ฅผ ํตํด ์์ฑ๋ ๊ฐ์ฒด๋ฅผ ์ธ์คํด์ค๋ผ๊ณ ๋ถ๋ฆ ๋๋ค.
class๋ ์๋ก์ด instance๋ฅผ ์์ฑํ ๋๋ง๋ค constructor()๋ฉ์๋๋ฅผ ํธ์ถํฉ๋๋ค.class Car { // Car ๊ฐ ํด๋์ค๋ฅผ ํตํด ์์ฑ๋ ๊ฐ์ฒด constructor(name, price) { this.name = name; this.price = price; } }
- Car๋ class์ด๋ฆ์ ๋๋ค. ํญ์ ๋๋ฌธ์๋ก ์์ํ๊ณ , CamelCase๋ก ์์ฑํด์ผ ํฉ๋๋ค.
- Car class์ instance๋ฅผ ์์ฑํ ๋๋ง๋ค constructor๋ฉ์๋๊ฐ ํธ์ถ๋ฉ๋๋ค.
- constructor()๋ฉ์๋๋ name, price 2๊ฐ์ argument(์ธ์)๋ฅผ ๋ฐ์ต๋๋ค.
- constructor()๋ฉ์๋์ this ํค์๋๋ฅผ ์ฌ์ฉํ์ต๋๋ค. class์ ์คํ๋ฒ์(context)์์ this๋ ํด๋น instance๋ฅผ ์๋ฏธํฉ๋๋ค.
- constructor()์์ ์ธ์๋ก ๋์ด์ค๋ name๊ณผ price๋ฅผ ์ฌ์ฉํด Car instance์ name, price ํ๋กํผํฐ์ ๊ฐ์ ํ ๋นํ์ต๋๋ค.
- ์ด๋ ๊ฒ ํด๋์ค ๋ด์์ name, price์ ๊ฐ์ด ๋ณ๊ฒฝ ๊ฐ๋ฅํ ์ํ๊ฐ์ด์ class๋ด์ ์ปจํ ์คํธ์์ ์ด๋ ๊ณณ์์๋ ์ฌ์ฉํ ์ ์๋ ๋ณ์๋ฅผ '๋ฉค๋ฒ ๋ณ์'๋ผ๊ณ ๋ถ๋ฆ ๋๋ค.
- ๋ฉค๋ฒ ๋ณ์๋ 'this' ํค์๋๋ก ์ ๊ทผํฉ๋๋ค.
๐ฆจ ์ธ์คํด์ค(Instance)
์์์ class instance๋ฅผ ์์ฑํ์ต๋๋ค.
์ธ์คํด์ค(Instance)๋ class๋ฅผ ํตํด ์์ฑ๋ ๊ฐ์ฒด์ ๋๋ค.
์ธ์คํด์ค๋ class์ property์ด๋ฆ๊ณผ method๋ฅผ ๊ฐ๋ ๊ฐ์ฒด์ ๋๋ค.
์ธ์คํด์ค ๋ง๋ค ๋ชจ๋ ๋ค๋ฅธ property ๊ฐ์ ๊ฐ๊ณ ์์ต๋๋ค.const morning = new Car('Morning', 20000000);
- ์ธ์คํด์ค๋ Class ์ด๋ฆ์ new๋ฅผ ๋ถ์ฌ์ ์์ฑํฉ๋๋ค.
- ํด๋์ค ์ด๋ฆ ์ฐ์ธก์ () ๊ดํธ๋ฅผ ์ด๊ณ ๋ซ๊ณ , ๋ด๋ถ์๋ constructor์์ ํ์ํ ์ ๋ณด๋ฅผ ์ธ์๋ก ๋๊ฒจ์ค๋๋ค.
- Carํด๋์ค์ instance๋ฅผ morning์ด๋ผ๋ ๋ณ์์ ์ ์ฅํ์ต๋๋ค.
- ๋ค์ ํ ๋ฒ! Car ํด๋์ค์ ์๋ก์ด instance๋ฅผ ์์ฑํ๋ ค๋ฉด new ํค์๋๊ฐ ํ์ํฉ๋๋ค. newํค์๋๋ constructor() ๋ฉ์๋๋ฅผ ํธ์ถํ๊ณ ์๋ก์ด instance๋ฅผ returnํด์ค๋๋ค.
- 'Morning'์ด๋ผ๋ String๊ณผ 2000000์ด๋ผ๋ Number๋ฅผ Car ์์ฑ์์ ๋๊ฒจ์ฃผ์๊ณ , name, price ํ๋กํผํฐ์ ๊ฐ์์ ๊ฐ์ด ํ ๋น๋์์ต๋๋ค.
์๋ก์ด ์ฐจ ๋ฑ๋กํ๊ธฐ.const spaceship = new Car('์ฐ์ฃผ์ ', 25000000); console.log(spaceship); console.log(spaceship.name); console.log(spaceship.price); console.log(spaceship.applyDiscount(0.5));
๐ฆจ ๋ฉ์๋(Methods)
๋ฉ์๋๋ ํจ์์ ๋๋ค.
๊ทธ๋ฐ๋ฐ ๊ฐ์ฒด๊ฐ ํ๋กํผํฐ ๊ฐ์ผ๋ก ๊ฐ๊ณ ์๋ ๊ฒ์ ๋ฉ์๋๋ผ๊ณ ๋ถ๋ฆ ๋๋ค.
Class์ method๋ Object(๊ฐ์ฒด)์ ๋ฌธ๋ฒ๊ณผ ๋๊ฐ์ต๋๋ค.
๋ค๋ง ๊ฐ์ฒด๋ ํ๋กํผํฐ๋ง๋ค comma(,)๋ก ๊ตฌ๋ถํด์ค์ผ ํ์ง๋ง, ํด๋์ค๋ ๊ทธ๋ ์ง ์์ต๋๋ค.
Car ๊ฐ์ฒด์ changeDepartment ๋ฉ์๋๋ฅผ ์ถ๊ฐํ์ต๋๋ค.class Car { constructor(name, price) { this.name = name; this.price = price; this.department = "์ ๋ฆ์ง์ "; } applyDiscount(discount) { return this.price * discount; } changeDepartment(departmentName) { this.department = departmentName; } } const spaceship = new Car('์ฐ์ฃผ์ ', 25000000); spaceship; /* ๊ฒฐ๊ณผ Car { name: '์ฐ์ฃผ์ ', price: 25000000, department: '์ ๋ฆ์ง์ ', __proto__: Car { constructor: ฦ Car(), applyDiscount: ฦ applyDiscount(), changeDepartment: ฦ changeDepartment() } } */ spaceship.name; //์ฐ์ฃผ์ spaceship.price; //25000000
changeDepartment ๋ฉ์๋๋ฅผ ํธ์ถํด๋ณด๊ณ ํด๋น ์ธ์คํด์ค์ department ์ ๋ณด๊ฐ ๋ฐ๋์๋์ง ํ์ธํด๋ด ์๋ค.
๐งโโ๏ธ this?
๐ฆจ Assignment
class ์์ฑ์ ์ฐ์ตํด๋ณด๊ฒ ์ต๋๋ค.
- MyMath๋ผ๋ class๋ฅผ ์์ฑํด์ฃผ์ธ์.
- constructor์์๋ ์ซ์ 2๊ฐ๋ฅผ ์ธ์๋ก ๋ฐ์ ํ๋กํผํฐ๋ก ์ ์ฅํฉ๋๋ค.
- ์ด 4๊ฐ์ ๋ฉ์๋๋ฅผ ๊ตฌํํด์ฃผ์ธ์. getNumber: ๋ ๊ฐ์ ์ซ์๊ฐ ๋ฌด์์ธ์ง ๋ฐฐ์ด๋ก ๋ฐํํ๋ ๋ฉ์๋ ex) [1, 2]
- add: ๋ ๊ฐ์ ์ซ์๋ฅผ ๋ํ๋ ๋ฉ์๋
- substract: ๋ ๊ฐ์ ์ซ์๋ฅผ ๋นผ๋ ๋ฉ์๋
- multiply: ๋ ๊ฐ์ ์ซ์๋ฅผ ๊ณฑํ๋ ๋ฉ์๋
๋ต๋ณ:
class MyMath { constructor(number1, number2) { this.number1 = number1; this.number2 = number2; } getNumber(){ let arr = []; arr.push(this.number1); arr.push(this.number2); return arr; } add(){ return this.number1 + this.number2; } substract() { return this.number1 - this.number2; } multiply(){ return this.number1 * this.number2; } }