ES6 λΆν° μΆκ° λμμ΅λλ€.
μ μ
- ν¨μμ ν μ’ λ₯μ΄λ©°, κ°μ²΄λ₯Ό μμ±νκΈ° μν ν νλ¦Ώ(ν) μ λλ€.
- ν΄λμ€ μμλ μμ±(field), νλ(method) μ΄ μμ΅λλ€.
- class λ κ°μ²΄μ λ°μ΄ν° μ μ΄λ₯Ό μ‘°μνλ λ©μλ λ₯Ό νλλ‘ μΆμν ν©λλ€.
λ¬Έλ²
class Person{
constructor(name, age){
// μμ±
this.name = name; // κ°μ²΄λ₯Ό μ΄κΈ°ν νκΈ° μν κ°
this.age = age;
}
// νλ
speak(){
console.log(`${this.name} : hello`)
}
}
// object , μλ‘μ΄ objectλ₯Ό λ§λ€ λ new ν€μλλ₯Ό μ¬μ© ν©λλ€.
const hyunho = new Person('hyunho', 26)
console.log(hyunho.name); // hyunho μΆλ ₯
console.log(hyunho.age); // 26 μΆλ ₯
hyunho.speak(); // hyunho : hello μΆλ ₯
- class ν€μλλ₯Ό μ¬μ©
- λ΄λΆμ constructor μμΉ ν©λλ€. -> κ°μ²΄λ₯Ό λ§λ€μ΄μ£Όλ μμ±μ λ©μλ μ λλ€.
κ°μ²΄(object)
물리μ μΌλ‘ μ‘΄μ¬νκ±°λ μΆμμ μΌλ‘ μκ° ν μ μλ κ² μ€μμ μμ μ μμ±μ κ°μ§κ³ μκ³ λ€λ₯Έ κ²κ³Ό μλ³ κ°λ₯ν κ²
- new μ ν¨κ» νΈμΆνμ§ μμΌλ©΄ μλ¬κ° λ°μ ν©λλ€.
Getter and Setter
νμ¬ μν©
class User {
constructor(firstName, lastName, age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
}
const user1 = new User("hyunho", "choi", -1);
console.log(user1);
- μ¬λμ λμ΄κ° -1 λΌλ κ²μ λΆκ°λ₯ ν©λλ€.
- μμ μμ μ²λΌ ν΄λμ€λ₯Ό μ¬μ©νλ μ¬μ©μκ° μλͺ» μ¬μ©μ νμμ λ
λ°©μ΄μ μΈ μμΈλ‘ λ°κΏμ£Όλ κ²μ΄ getter μ setter μ λλ€. - get : κ°μ λ¦¬ν΄ ν©λλ€.
- set(value) : κ°μ μ€μ ν©λλ€. setμ κ°μ μ€μ νκΈ° λλ¬Έμ valueλ₯Ό λ°μμΌ ν©λλ€.
νμ§λ§ λ¨μν get κ³Ό set μ μ¬μ©νλ©΄ μ€λ₯κ° λ°μ ν©λλ€.
class User {
constructor(firstName, lastName, age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
get age(){
return this.age;
}
set age(value){
this.age = value;
}
}
μ€λ₯κ° μΌμ΄λλ μ΄μ
- age λΌλ get λ₯Ό μ μνλ μκ° this.ageλ λ©λͺ¨λ¦¬μ μ¬λΌκ° μλ λ°μ΄ν°λ₯Ό μ½μ΄μ€λ κ²μ΄ μλλΌ λ°λ‘ get νΈμΆ ( μ£Όν©μ λ°μ€ 보기 )
- set μ μ μνλ μκ° ( 보λΌμ λ°μ€ 보기 ) = age λ₯Ό νΈμΆ ν λ μ¦. κ°μ ν λΉ ν λ λ°λ‘ λ©λͺ¨λ¦¬μ κ°μ ν λΉνλ κ²μ΄ μλλΌ set μμμ μ§μμ μΈ νΈμΆμ΄ μΌμ΄λ©λλ€.
μλ§κ² μ¬μ©νλ λ°©λ²
class User {
constructor(firstName, lastName, age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
get age() {
return this._age;
}
set age(value) {
this._age = value;
}
}
const user1 = new User("hyunho", "choi", -1);
console.log(user1);
- get κ³Ό set μμμ μ°μ¬μ§λ λ³μ μ΄λ¦μ μ‘°κΈ λ€λ₯΄κ² ν΄μ€μΌν©λλ€. ( λ³΄ν΅ _ μ¬μ©νμ¬ μ‘°κΈ λ€λ₯΄κ² ν©λλ€. )
μμ
- κ³΅ν΅ μμλ₯Ό λ²κ±°λ‘κ² μμ±ν νμμμ΄ λμΌν κ²λ€μ κ³μ μ¬μ¬μ© ν μ μμ΅λλ€.
- extends ν€μλ μ¬μ©
class Shape {
constructor(width, height, color) {
this.width = width;
this.height = height;
this.color = color;
}
draw() {
console.log(`drawing ${this.color} color`);
}
area() {
return this.width * this.height;
}
}
class Rectangle extends Shape {}
const rectangle = new Rectangle(10, 10, "red");
rectangle.draw(); // drawing red color μΆλ ₯
- class Rectangle extends Shape {} ν μ€λ‘ μΈνμ¬ shape μ μλ λͺ¨λ κ²λ€μ΄ rectangle μ ν¬ν¨μ΄ λ©λλ€.
λ€νμ± ( μ€λ²λΌμ΄λ© )
- νμν ν¨μλ§ μ¬μ μν΄μ μ¬μ©ν μ μμ΅λλ€.
- λΆλͺ¨ λ©μλλ₯Ό ν λλ‘ μΌλΆ κΈ°λ₯λ§ λ³κ²½νκ³ μΆμ λ, λλ λΆλͺ¨μ λ©μλμ κΈ°λ₯μ νμ₯νκ³ μΆμ λ μ¬μ© ν©λλ€.
- μ€λ²λΌμ΄λ© λ ν¨μκ° νΈμΆλλ©΄ μ΄μ μ μ‘΄μ¬νλ ν¨μλ νΈμΆνμ§ μκ² λ©λλ€. ( μ¬μ μν ν¨μλ§ νΈμΆ )
- νμ§λ§, μ€λ²λΌμ΄λ©ν ν¨μλ μΆλ ₯νκ³ κ³΅ν΅μ μΌλ‘ μ μν ν¨μλ νΈμΆνκ³ μΆλ€λ©΄ μ΄λ΄ λ, λΆλͺ¨μ ν¨μλ₯Ό νΈμΆνλ super λ₯Ό μ¬μ© ν©λλ€.
λνμ λλΉλ₯Ό ꡬνλ μν©μ κ°μ
class Shape {
constructor(width, height, color) {
this.width = width;
this.height = height;
this.color = color;
}
draw() {
console.log(`drawing ${this.color} color`);
}
area() {
return this.width * this.height;
}
}
class Rectangle extends Shape {}
class Triangle extends Shape {}
const rectangle = new Rectangle(10, 10, "red");
console.log(rectangle.area()); // 100
const triangle = new Triangle(20, 20, "blue");
console.log(triangle.area()); // 400
- μ¬κ°νμ λλΉλ κ°λ‘ * μΈλ‘ μ΄λ―λ‘ μ¬λ°λ₯΄κ² μΆλ ₯ λ©λλ€.
- νμ§λ§, μΌκ°νμ λλΉλ κ°λ‘ * μΈλ‘ μΈκ°? μλλλ€. (κ°λ‘*μΈλ‘) / 2 μ΄λ κ² λμΌ ν©λλ€.
μ¬μ μ
class Shape {
constructor(width, height, color) {
this.width = width;
this.height = height;
this.color = color;
}
draw() {
console.log(`drawing ${this.color} color`);
}
area() {
return this.width * this.height;
}
}
class Rectangle extends Shape {}
class Triangle extends Shape {
area() {
return (this.width * this.height) / 2; // μ¬μ μ
}
}
const rectangle = new Rectangle(10, 10, "red");
console.log(rectangle.area()); // 100
const triangle = new Triangle(20, 20, "blue");
console.log(triangle.area()); // 200
μμ 2
class Shape {
constructor(width, height, color) {
this.width = width;
this.height = height;
this.color = color;
}
draw() {
console.log(`drawing ${this.color} color`);
}
area() {
return this.width * this.height;
}
}
class Rectangle extends Shape {}
class Triangle extends Shape {
draw() {
console.log(`Good`); // good μΆλ ₯, μ¬μ μ λμκΈ° λλ¬Έμ
//λΆλͺ¨μ console.log(`drawing ${this.color} color`); μΆλ ₯νμ§ μμ΅λλ€.
}
area() {
return (this.width * this.height) / 2;
}
}
const rectangle = new Rectangle(10, 10, "red");
console.log(rectangle.area());
const triangle = new Triangle(20, 20, "blue");
console.log(triangle.area());
triangle.draw();
- νμ§λ§ λΆλͺ¨μ draw() μ κ°μ΄ μΆλ ₯νκ³ μΆλ€λ©΄?
super μ¬μ©
class Shape {
constructor(width, height, color) {
this.width = width;
this.height = height;
this.color = color;
}
draw() {
console.log(`drawing ${this.color} color`);
}
area() {
return this.width * this.height;
}
}
class Rectangle extends Shape {}
class Triangle extends Shape {
draw() {
super.draw();
console.log(`Good`);
}
area() {
return (this.width * this.height) / 2;
}
}
const rectangle = new Rectangle(10, 10, "red");
console.log(rectangle.area());
const triangle = new Triangle(20, 20, "blue");
console.log(triangle.area());
triangle.draw();
μ°Έκ³
'π Front-End > JavaScript' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
AJAX (0) | 2022.11.17 |
---|---|
ν΄λ‘μ (closure) (0) | 2022.11.16 |
μμ±μ ν¨μ (0) | 2022.11.16 |
Event Loop (0) | 2022.11.16 |
μ΄λ²€νΈ λ²λΈλ§, μ΄λ²€νΈ μΊ‘μ²λ§, μ΄λ²€νΈ μμ (0) | 2022.11.16 |