๐ฆ Day28
๐ข ์ด ๊ธ์ ๋๋ฆผ์ฝ๋ฉ ์๋ฆฌ๋์ ๊ฐ์ฒด ์งํฅ ์ธ์ด ํด๋์ค ์ ๋ฆฌ ์ ํ๋ธ ๊ฐ์ ๋ด์ฉ์ ์ ๋ฆฌํ ๊ธ์ ๋๋ค.
ํด๋์ค ์์ฒด์๋ ๋ฐ์ดํฐ๊ฐ ๋ค์ด์์ง ์๊ณ ํฐ ํ, template๋ง ์ ํด๋๋๋ค.
์ด๋ ์ด๋ ํ ๋ฐ์ดํฐ๊ฐ ๋ค์ด์ฌ ์ ์๋์ง๋ง ์ ์ํ ํ ์ต์ด ํ๋ฒ๋ง ์ ์ธํ๋ค.
ํด๋์ค๋ฅผ ์ด์ฉํด ์ค์ ๋ฐ์ดํฐ๋ฅผ ๋ฃ์ด์ ๋ง๋๋ ๊ฒ์ด object ์ฆ, ์ธ์คํด์ค์ด๋ค.
๋ถ์ด๋นตํ ๋น์
Class โ ๋ถ์ด๋นตํ
object โ ๋ถ์ด๋นต
ํด๋์ค ๊ฐ๋ ์ ES6์ ๋์ ๋์๋ค. ES6 ์ ์๋ class๊ฐ ์๋ function์ ์ด์ฉํ์ฌ object๋ฅผ ๋ง๋ค์๋ค.
ํด๋์ค ๋ชธ์ฒด์๋ ์์ฑ(fields)
๊ณผ ํ๋(method)
์ด ์ ์๋๋ค. fields๋ง ์๋ ํด๋์ค๋ ๋ฐ์ดํฐ ํด๋์ค
๋ผ๊ณ ๋ถ๋ฅธ๋ค.
class Person{
//constructor
constructor(name, age) { // ์์ฑ์. object๋ฅผ ๋ง๋ค๋ ํ์ํ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ๋ค.
//fields
// ์ ๋ฌ๋ฐ์ ๋ฐ์ดํฐ๋ฅผ fields์ ํ ๋นํด์ค๋ค.
this.name = name;
this.age = age;
}
// methods
speak() {
console.log(`${this.name}: hello!`);
}
}
const tina = new Person('tina', 20);
console.log(tina.name); // tina
console.log(tina.age); // 20
tina.speak(); // tina: hello!
Getters
์ Setters
class User {
constructor(firstName, lastName, age){
// fields - firstName, lastName, _age
this.firstname = firstName;
this.lastName = lastName;
this.age = age; // ์๋์ getter๊ฐ ์ ์๋์ด์์ผ๋ฏ๋ก this.age๊ฐ getter๋ฅผ ํธ์ถ
// ์๋์ setter๊ฐ ์ ์๋์ด์์ผ๋ฏ๋ก age๋ฅผ this.age์ ํ ๋นํ ๋ setter๋ฅผ ํธ์ถํ๋ค
}
get age() {
return this._age;
}
set age(value) { // ๊ฐ์ ์ค์ ํ๊ธฐ ์ํด์ ๋งค๊ฐ๋ณ์๋ฅผ ํตํด ๋ฐ์ดํฐ๋ฅผ ๋ฐ์์จ๋ค
// value๋ฅผ ์ขํญ ๋ณ์์ ํ ๋นํ ๋ setter์ธ ์๊ธฐ์์ ์ ๋ ํธ์ถํ๋ฉด์ call stack์ด ๊ฝ ์ฐจ๊ฒ ๋๋ค.
// ์ด๋ฅผ ๋ฐฉ์งํ๊ธฐ ์ํด ๋ณ์ ์ด๋ฆ์ _age ๋ก ๋ฐ๊ฟ์ค๋ค.
// value๋ก ๋ฐ์ ๋ฐ์ดํฐ๋ฅผ this._age์ ํ ๋น
this._age = value < 0 ? 0: value;
// ์์ ๊ฐ์ ๋ฃ์ผ๋ฉด 0 ๊ฐ์ ๋ฃ์ด์ฃผ๊ณ ์์ ๊ฐ์ ๋ฃ์์๋๋ ๊ทธ ๊ฐ ๊ทธ๋๋ก age์ ํ ๋นํ๋ค.
}
}
const user1 = new User('Richard', 'Madden', -1); // ์ฌ์ฉ์๊ฐ ์ค์๋ก ๋์ด๋ฅผ -1๋ก ์
๋ ฅํ ๊ฒฝ์ฐ
console.log(user1.age); // 0
class Shape {
constructor(width, height, color){
this.width = width;
this.height = height;
this.color = color;
}
draw() {
console.log(`drawing ${this.color} color!`);
}
getArea() {
return this.width * this.height;
}
}
class Rectangle extends Shape {} // Shape ํด๋์ค์ fields, method๋ฅผ ์์๋ฐ๋๋ค. ์ฆ, Shape ํด๋์ค๊ฐ ๋ถ๋ชจ ํด๋์ค๊ฐ ๋๋ค.
class Triangle extends Shape { // Shape ํด๋์ค๊ฐ ๋ถ๋ชจ ํด๋์ค๊ฐ ๋๋ค.
draw() { // overriding
super.draw(); // ๋ถ๋ชจ class์ธ Shape์ draw()ํจ์ ์ฌ์ฉ
console.log('๐บ');
}
getArea() { // overriding
return (this.width * this.height) / 2;
}
}
const rectangle = new Rectangle(20, 20, 'blue');
rectangle.draw();
console.log('์ฌ๊ฐํ ๋์ด', rectangle.getArea());
const triangle = new Triangle(20, 20, 'red');
triangle.draw();
console.log('์ผ๊ฐํ ๋์ด', triangle.getArea());
instanceof ์ฐ์ฐ์์ ์ข์ธก์ ์ ๋ object๊ฐ ์ฐ์ธก์ ์ ๋ class์ instance์ธ์ง ํ์ธํ์ฌ boolean ๊ฐ์ผ๋ก ๋ฆฌํดํด์ค๋ค.
// ์ ์์ ์ฝ๋์์ ๋ง๋ ํด๋์ค์ ์ธ์คํด์ค๋ฅผ ์์๋ก ๋ค์์๋
console.log(rectangle instanceof Rectangle); // true
console.log(triangle instanceof Rectangle); // false
console.log(triangle instanceof Triangle); // true
console.log(triangle instanceof Shape); // true
console.log(triangle instanceof Object); // true
// ์๋ฐ์คํฌ๋ฆฝํธ์์ ๋ง๋ ๋ชจ๋ object๋ค์ Object๋ฅผ ์์ํ ๊ฒ์ด๋ค
์ฐธ๊ณ ์๋ฃ
๋๋ฆผ์ฝ๋ฉ by ์๋ฆฌ | ์๋ฐ์คํฌ๋ฆฝํธ 6. ํด๋์ค์ ์ค๋ธ์ ํธ์ ์ฐจ์ด์ (class vs object), ๊ฐ์ฒด์งํฅ ์ธ์ด ํด๋์ค ์ ๋ฆฌ | ํ๋ก ํธ์๋ ๊ฐ๋ฐ์ ์
๋ฌธํธ (JavaScript ES6)
https://www.youtube.com/watch?v=_DLhUBWsRtw
์ค ์ค๋์ ๋๋ฆผ์ฝ๋ฉ ์๋ฆฌ๋ ์์์ ๋ณด์ จ๊ตฐ์!! ์์๊ณผ ๋คํ์ฑ, ๊ฒํฐ์ ์ธํฐ๋ ์ ๋ ๋ค์ ๊ณต๋ถํด๋ด์ผ๊ฒ ์ด์! ์ ๋ฆฌํด๋์ผ์ ๊ฑฐ ๋ณด๋๊น ๋ ์๋กญ๋ค์^ใ .. ์ค๋๋ ์๊ณ ํ์ จ์ต๋๋ค! ๋ด์ผ๋ ๊ฐ์ด ํ๋ด๋ด์ โช(ยดโฝ๏ฝ)