π λͺ¨λ μλ°μ€ν¬λ¦½νΈ κ΅μ¬ 25μ₯. ν΄λμ€
01. ν΄λμ€λ νλ‘ν νμ μ λ¬Έλ²μ μ€νμΈκ°?
μλ°μ€ν¬λ¦½νΈλ νλ‘ν νμ
κΈ°λ° κ°μ²΄μ§ν₯ μΈμ΄λ€.
νλ‘ν νμ
κΈ°λ° κ°μ²΄μ§ν₯ μΈμ΄λ ν΄λμ€κ° νμ μλ(class free)κ°μ²΄μ§ν₯ νλ‘κ·Έλλ° μΈμ΄λ€.
ES5μμλ ν΄λμ€ μμ΄λ μμ±μ ν¨μμ νλ‘ν νμ
μ ν΅ν΄ κ°μ²΄μ§ν₯ μΈμ΄μ μμμ ꡬνν μ μλ€.
π‘ ES6μμ ν΄λμ€ μΆκ°λ¨
β‘οΈ ν΄λμ€λ ν¨μμ΄λ©° κΈ°μ‘΄ νλ‘ν νμ κΈ°λ° ν¨ν΄μ ν΄λμ€ κΈ°λ° ν¨ν΄μ²λΌ μ¬μ©ν μ μλλ‘ νμ¬, λ¬Έλ²μ μ€ν(syntactic sugar)μ΄λΌκ³ νννλ€.
π‘ ν΄λμ€μ μμ±μ ν¨μμ μ°¨μ΄μ
- ν΄λμ€λ₯Ό newμ°μ°μ μμ΄ νΈμΆνλ©΄ μλ¬κ° λ°μνλ€.(μμ±μ ν¨μλ₯Ό newμ°μ°μ μμ΄ νΈμΆνλ©΄ μΌλ° ν¨μλ‘ νΈμΆλλ€.)
- ν΄λμ€λ μμμ μ§μνλ extendsμ superν€μλλ₯Ό μ 곡νλ€.(μμ±μ ν¨μλ μ§μνμ§ μλλ€.)
- ν΄λμ€λ νΈμ΄μ€ν μ΄ λ°μνμ§ μλ κ²μ²λΌ λμνλ€.(ν¨μ μ μΈλ¬Έμ ν¨μ νΈμ΄μ€ν , ν¨μ ννμμ λ³μ νΈμ΄μ€ν μ΄ λ°μνλ€.)
- ν΄λμ€ λ΄μ λͺ¨λ μ½λμλ μ묡μ μΌλ‘ strict modeκ° μ§μ λμ΄ μ€νλλ©° ν΄μ ν μ μλ€.(μμ±μ ν¨μλ μ§μ λμ§ μλλ€.)
- ν΄λμ€μ constructor, νλ‘ν νμ λ©μλ, μ μ λ©μλλ μ΄κ±°λμ§ μλλ€.(λͺ¨λ νλ‘νΌν° μ΄νΈλ¦¬λ·°νΈ [[Enumerable]]μ κ°μ΄ falseλ€.)
02. ν΄λμ€ μ μ
- ν΄λμ€λ classν€μλλ₯Ό μ¬μ©νμ¬ μ μνλ€.
- ν΄λμ€λ ν¨μλ€. λ°λΌμ ν΄λμ€λ κ°μ²λΌ μ¬μ©ν μ μλ μΌκΈ κ°μ²΄λ€.
- ν΄λμ€ λͺΈμ²΄λ 0κ° μ΄μμ λ©μλλ§ μ μν μ μλ€. (constructor(μμ±μ), νλ‘ν νμ λ©μλ, μ μ λ©μλ)
π₯οΈ μμ μ½λ 25-04
// ν΄λμ€ μ μΈλ¬Έ
class Person {
// μμ±μ
constructor(name) {
// μΈμ€ν΄μ€ μμ± λ° μ΄κΈ°ν
this.name = name; // name νλ‘νΌν°λ publicνλ€.
}
// νλ‘ν νμ
λ©μλ
sayHi() {
console.log(`Hi! My name is ${this.name}`);
}
// μ μ λ©μλ
static sayHello() {
console.log('Hello!');
}
}
// μΈμ€ν΄μ€ μμ±
const me = new Person('Lee');
// μΈμ€ν΄μ€μ νλ‘νΌν° μ°Έμ‘°
console.log(me.name); // Lee
// νλ‘ν νμ
λ©μλ νΈμΆ
me.sayHi(); // Hi! My name is Lee
// μ μ λ©μλ νΈμΆ
Person.sayHello(); // Hello!
03. ν΄λμ€ νΈμ΄μ€ν
π‘ ν΄λμ€λ ν¨μλ‘ νκ°λλ€.
β‘οΈ ν¨μ μ μΈλ¬Έμ²λΌ, λ°νμ μ΄μ μ λ¨Όμ νκ°λμ΄ ν¨μ κ°μ²΄λ₯Ό μμ±νλ€.
π‘ ν΄λμ€ μ μΈλ¬Έμ νΈμ΄μ€ν μ΄ λ°μνλ€.
π₯οΈ μμ μ½λ 25-07
const Person = '';
{
// νΈμ΄μ€ν
μ΄ λ°μνμ§ μλλ€λ©΄ ''μ΄ μΆλ ₯λμ΄μΌ νλ€.
console.log(Person);
// ReferenceError: Cannot access 'Person' before initialization
// ν΄λμ€ μ μΈλ¬Έ
class Person {}
}
β‘οΈ λ€λ§, ν΄λμ€ μ μΈλ¬Έ μ΄μ μ μΌμμ μ¬κ°μ§λμ λΉ μ§κΈ° λλ¬Έμ νΈμ΄μ€ν μ΄ λ°μνμ§ μλ κ²μ²λΌ λμνλ€.
(*μΌμμ μ¬κ°μ§λ : μ μΈ λ¨κ³μ μ΄κΈ±ν λ¨κ³ μ¬μ΄μμλ λ³μλ₯Ό λ±λ‘νμ§λ§ λ©λͺ¨λ¦¬κ° ν λΉλμ§ μμ μνλΌ "ReferenceError"κ° λμ€κ² λ¨)
β‘οΈ κ²°λ‘ μ ν΄λμ€ μ μΈλ¬Έμ λ°νμ μ΄μ μ λ¨Όμ μ€νλκΈ° λλ¬Έμ νΈμ΄μ€ν μ΄ λ°μνλ€.
04. μΈμ€ν΄μ€ μμ±
ν΄λμ€λ μμ±μ ν¨μμ΄λ©° newμ°μ°μμ ν¨κ» νΈμΆλμ΄ μΈμ€ν΄μ€λ₯Ό μμ±νλ€.
β ν΄λμ€λ newμ°μ°μμ ν¨κ» μμ±μ ν¨μλ‘ νΈμΆλμ΄μΌλ§ μΈμ€ν΄μ€λ₯Ό μμ±ν μ μλ€. (λ€λ₯Έ λ°©λ²μ΄ X)
π₯οΈ μμ μ½λ 25-09 <μΈμ€ν΄μ€ μμ±>
class Person {}
// μΈμ€ν΄μ€ μμ±
const me = new Person();
console.log(me); // Person {}
π‘ ν΄λμ€ ννμμΌλ‘ μ μλ ν΄λμ€μ κ²½μ° μλ³μ(Person)λ₯Ό μ¬μ©ν΄μΌνλ€.
β οΈ κΈ°λͺ ν΄λμ€μ ν΄λμ€ μ΄λ¦(MyClass)μ μ¬μ©ν΄ μμ±νλ €κ³ νλ©΄ μλ¬κ° λ°μνλ€.
π₯οΈ μμ μ½λ 25-10
const Person = class MyClass {};
// ν¨μ ννμκ³Ό λ§μ°¬κ°μ§λ‘ ν΄λμ€λ₯Ό κ°λ¦¬ν€λ μλ³μλ‘ μΈμ€ν΄μ€λ₯Ό μμ±ν΄μΌ νλ€.
const me = new Person();
// ν΄λμ€ μ΄λ¦ MyClassλ ν¨μμ λμΌνκ² ν΄λμ€ λͺΈμ²΄ λ΄λΆμμλ§ μ ν¨ν μλ³μλ€.
console.log(MyClass); // ReferenceError: MyClass is not defined
const you = new MyClass(); // ReferenceError: MyClass is not defined
05. λ©μλ
π‘ ν΄λμ€ λͺΈμ²΄μ μ μν μ μλ λ©μλ
- constructor(μμ±μ)
- νλ‘ν νμ λ©μλ
- μ μ λ©μλ
1) constructor
- constructorλ μΈμ€ν΄μ€λ₯Ό μμ±νκ³ μ΄κΈ°ννκΈ° μν νΉμν λ©μλλ€.
- constructorλ μ΄λ¦μ λ³κ²½ν μ μλ€.
- ν΄λμ€κ° νκ°λμ΄ μμ±λ ν¨μ κ°μ²΄λ ν΄λμ€κ° μμ±ν μΈμ€ν΄μ€μλ constructorλ©μλκ° μ‘΄μ¬νμ§ μλλ€.(λ¨μν λ©μλκ° μλ)
- constructorλ λ©μλλ‘ ν΄μλμ§ μκ³ ν΄λμ€κ° νκ°λμ΄ μμ±ν ν¨μ κ°μ²΄ μ½λμ μΌλΆκ° λλ€.
- constructorλ μλ΅ κ°λ₯νλ€.(μΈμ€ν΄μ€λ₯Ό μ΄κΈ°ννλ €λ©΄ μλ΅νλ©΄ μλλ€. μλ? constructorλ μΈμ€ν΄μ€λ₯Ό μ΄κΈ°ννκΈ° μν λ©μλμ΄λ―λ‘)
- constructorλ ν΄λμ€ λ΄μ μ΅λ ν κ°λ§ μ‘΄μ¬ν μ μλ€.
- νλ‘νΌν°κ° μΆκ°λμ΄ μ΄κΈ°νλ μΈμ€ν΄μ€λ₯Ό μμ±νλ €λ©΄ constructor λ΄λΆμμ thisμ μΈμ€ν΄μ€ νλ‘νΌν°λ₯Ό μΆκ°νλ€.(μ΄λλ constructorλ₯Ό μλ΅νλ©΄ μλλ€.)
- constructor λ΄λΆμμ μμ±μ ν¨μμ λμΌνκ² μ묡μ μΌλ‘ thisλ₯Ό λ°ννλ€.(μ¦ λ€λ₯Έ κ°μ²΄λ₯Ό λͺ μμ μΌλ‘ λ°ννλ©΄ μλ¨. μμ±μ ν¨μμ λμΌ)
2) νλ‘ν νμ λ©μλ
π‘ ν΄λμ€ λͺΈμ²΄μ μ μν λ©μλλ κΈ°λ³Έμ μΌλ‘ νλ‘ν νμ λ©μλκ° λλ€. (μμ±μ ν¨μμ μν κ°μ²΄ μμ± λ°©μκ³Όλ λ€λ₯΄κ² prototypeνλ‘νΌν°μ λ©μλλ₯Ό μΆκ°νμ§ μμλ λλ€.)
β‘οΈ ν΄λμ€κ° μμ±ν μΈμ€ν΄μ€λ νλ‘ν νμ 체μΈμ μΌμμ΄ λλ€.
π₯οΈ μμ μ½λ 25-22 <μμ±μ ν¨μμ νλ‘ν νμ λ©μλ μΆκ° λ°©μ>
// μμ±μ ν¨μ
function Person(name) {
this.name = name;
}
// νλ‘ν νμ
λ©μλ
Person.prototype.sayHi = function () {
console.log(`Hi! My name is ${this.name}`);
};
const me = new Person('Lee');
me.sayHi(); // Hi! My name is Lee
π₯οΈ μμ μ½λ 25-23 <ν΄λμ€μ λ©μλ μΆκ° λ°©μ>
class Person {
// μμ±μ
constructor(name) {
// μΈμ€ν΄μ€ μμ± λ° μ΄κΈ°ν
this.name = name;
}
// νλ‘ν νμ
λ©μλ
sayHi() {
console.log(`Hi! My name is ${this.name}`);
}
}
const me = new Person('Lee');
me.sayHi(); // Hi! My name is Lee
3) μ μ λ©μλ
μ μ (static)λ©μλλ μΈμ€ν΄μ€λ₯Ό μμ±νμ§ μμλ νΈμΆν μ μλ λ©μλλ₯Ό λ§νλ€.
π‘ μμ±μ ν¨μμ κ²½μ° λ€μκ³Ό κ°μ΄ λͺ μμ μΌλ‘ μμ±μ ν¨μμ λ©μλλ₯Ό μΆκ°ν΄ μ μ λ©μλλ₯Ό μμ±νλ€.
π₯οΈ μμ μ½λ 25-25
// μμ±μ ν¨μ
function Person(name) {
this.name = name;
}
// μ μ λ©μλ
Person.sayHi = function () {
console.log('Hi!');
};
// μ μ λ©μλ νΈμΆ
Person.sayHi(); // Hi!
π‘ ν΄λμ€μμλ staticν€μλλ₯Ό λΆμ΄λ©΄ μ μ λ©μλ(ν΄λμ€ λ©μλ)κ° λλ€.
π₯οΈ μμ μ½λ 25-26
class Person {
// μμ±μ
constructor(name) {
// μΈμ€ν΄μ€ μμ± λ° μ΄κΈ°ν
this.name = name;
}
// μ μ λ©μλ
static sayHi() {
console.log('Hi!');
}
}
<κ·Έλ¦Ό 25-5 νλ‘ν νμ μ²΄μΈ μ°Έκ³ >
π‘ μ μ λ©μλ(sayHi)λ ν΄λμ€μ λ°μΈλ©λ λ©μλκ° λλ€.
β οΈ ν΄λμ€λ ν΄λμ€ μ μ(ν΄λμ€ μ μΈλ¬Έ, ν΄λμ€ ννμ)κ° νκ°λλ μμ μ ν¨μ κ°μ²΄κ° λλ―λ‘ μΈμ€ν΄μ€μ λ¬λ¦¬ λ³λ€λ₯Έ μμ± κ³Όμ μ΄ νμ μλ€.
// μ μ λ©μλλ ν΄λμ€λ‘ νΈμΆνλ€.
// μ μ λ©μλλ μΈμ€ν΄μ€ μμ΄λ νΈμΆν μ μλ€.
Person.sayHi(); // Hi!
π‘ μ μ λ©μλλ μΈμ€ν΄μ€λ‘ νΈμΆν μ μλ€.(μλ? μ μ λ©μλκ° λ°μΈλ©λ ν΄λμ€λ μΈμ€ν΄μ€μ νλ‘ν νμ μ²΄μΈ μμ μ‘΄μ¬νμ§ μλλ€.)
// μΈμ€ν΄μ€ μμ±
const me = new Person('Lee');
me.sayHi(); // TypeError: me.sayHi is not a function
4) μ μ λ©μλμ νλ‘ν νμ λ©μλμ μ°¨μ΄
- μ μ λ©μλμ νλ‘ν νμ λ©μλλ μμ μ΄ μν΄μλ νλ‘ν νμ 체μΈμ΄ λ€λ₯΄λ€.
- μ μ λ©μλλ ν΄λμ€λ‘ νΈμΆνκ³ vs. νλ‘ν νμ λ©μλλ μΈμ€ν΄μ€λ‘ νΈμΆνλ€.
- μ μ λ©μλλ μΈμ€ν΄μ€ νλ‘νΌν°λ₯Ό μ°Έμ‘°ν μ μμ§λ§(μλ? μμμ λ§νλ―μ΄ μ μ λ©μλκ° λ°μΈλ©λ ν΄λμ€λ μΈμ€ν΄μ€ νλ‘ν νμ μ²΄μΈ μμ μ‘΄μ¬νμ§ μκΈ° λλ¬Έ) vs. νλ‘ν νμ λ©μλλ μΈμ€ν΄μ€ νλ‘νΌν°λ₯Ό μ°Έμ‘°ν μ μλ€.
π₯οΈ μμ μ½λ 25-29 <μ μ λ©μλ μμ >
class Square {
// μ μ λ©μλ
static area(width, height) {
return width * height;
}
}
console.log(Square.area(10, 10)); // 100
π₯οΈ μμ μ½λ 25-30 <νλ‘ν νμ λ©μλ μμ >
class Square {
constructor(width, height) {
this.width = width;
this.height = height;
}
// νλ‘ν νμ
λ©μλ
area() {
return this.width * this.height;
}
}
const square = new Square(10, 10);
console.log(square.area()); // 100
5) ν΄λμ€μμ μ μν λ©μλμ νΉμ§
- functionν€μλλ₯Ό μλ΅ν λ©μλ μΆμ½ ννμ μ¬μ©νλ€.
- κ°μ²΄ 리ν°λ΄κ³Όλ λ€λ₯΄κ² ν΄λμ€μ λ©μλλ₯Ό μ μν λλ μ½€λ§κ° νμ μλ€.
- μ묡μ μΌλ‘ strict modeλ‘ μ€νλλ€.
- for...inλ¬Έμ΄λ Object.keysλ©μλ λ±μΌλ‘ μ΄κ±°ν μ μλ€.([[Enumerable]]κ°μ΄ falseλ€.)
- λ΄λΆ λ©μλ [[Construct]]λ₯Ό κ°μ§ μλ non-constructorλ€.(newμ°μ°μμ ν¨κ» νΈμΆν μ μλ€.)
06. ν΄λμ€μ μΈμ€ν΄μ€ μμ± κ³Όμ
1. μΈμ€ν΄μ€ μμ±κ³Ό this λ°μΈλ©
- newμ°μ°μμ ν¨κ» ν΄λμ€λ₯Ό νΈμΆνλ©΄ μ묡μ μΌλ‘ λΉ κ°μ²΄(μΈμ€ν΄μ€)κ° μμ±λλ€.
- λΉ κ°μ²΄λ thisμ λ°μΈλ©λλ€.
2. μΈμ€ν΄μ€ μ΄κΈ°ν
- constructor λ΄λΆμ μ½λκ° μ€νλλ©° thisμ λ°μΈλ©λμ΄ μλ μΈμ€ν΄μ€λ₯Ό μ΄κΈ°ννλ€.
- μ¦, μΈμ€ν΄μ€μ νλ‘νΌν° μΆκ°νκ³ μ΄κΈ°ννλ€.
3. μΈμ€ν΄μ€ λ°ν
- ν΄λμ€μ λͺ¨λ μ²λ¦¬κ° λλλ©΄ μμ±λ μΈμ€ν΄μ€κ° λ°μΈλ©λ thisκ° μ묡μ μΌλ‘ λ°νλλ€.
π₯οΈ μμ μ½λ 25-32
class Person {
// μμ±μ
constructor(name) {
// 1. μ묡μ μΌλ‘ μΈμ€ν΄μ€κ° μμ±λκ³ thisμ λ°μΈλ©λλ€.
console.log(this); // Person {}
console.log(Object.getPrototypeOf(this) === Person.prototype); // true
// 2. thisμ λ°μΈλ©λμ΄ μλ μΈμ€ν΄μ€λ₯Ό μ΄κΈ°ννλ€.
this.name = name;
// 3. μμ±λ μΈμ€ν΄μ€κ° λ°μΈλ©λ thisκ° μ묡μ μΌλ‘ λ°νλλ€.
}
}
07. νλ‘νΌν°
1) μΈμ€ν΄μ€ νλ‘νΌν°
π₯οΈ μμ μ½λ 25-33
class Person {
constructor(name) {
// μΈμ€ν΄μ€ νλ‘νΌν°
this.name = name;
}
}
const me = new Person('Lee');
console.log(me); // Person {name: "Lee"}
constructor λ΄λΆμ thisλ ν΄λμ€κ° μμ±ν λΉ κ°μ²΄(μΈμ€ν΄μ€)κ° λ°μΈλ© λμ΄μλ€.
λ°λΌμ this.name = name;μ½λκ° μ€νλλ©΄ thisμ μΈμ€ν΄μ€ νλ‘νΌν°κ° μΆκ°λμ΄ μ΄κΈ°νλλ€.
2) μ κ·Όμ νλ‘νΌν°
μ κ·Όμ νλ‘νΌν°λ μ체μ μΌλ‘λ κ°([[Value]]λ΄λΆμ¬λ‘―)μ κ°μ§ μκ³ λ€λ₯Έ λ°μ΄ν° νλ‘νΌν°μ κ°μ μ½κ±°λ μ μ₯ν λ μ¬μ©νλ μ κ·Όμ ν¨μλ‘ κ΅¬μ±λ νλ‘νΌν°λ€.
- μ κ·Όμ νλ‘νΌν°λ ν΄λμ€μμλ μ¬μ©ν μ μλ€.
- μ κ·Όμ νλ‘νΌν°λ μ체μ μΌλ‘λ κ°μ κ°μ§ μκ³ μ½κ±°λ μ μ₯ν λ μ¬μ©νλ μ κ·Όμ ν¨μ(getter, setter)λ‘ κ΅¬μ±λμ΄ μλ€.
- getterλ λ©μλ μ΄λ¦ μμ getν€μλλ₯Ό μ¬μ©ν΄ μ μνλ€.
- setterλ λ©μλ μ΄λ¦ μμ setν€μλλ₯Ό μ¬μ©ν΄ μ μνλ€.
- getterμ setter μ΄λ¦μ μΈμ€ν΄μ€ νλ‘νΌν°μ²λΌ μ¬μ©λλ€.(ex me.fullName = 'Heegun Lee';, console.log(me.fullName);)
- getterλ 무μΈκ°λ₯Ό μ·¨λ€ν λ μ¬μ©νλ―λ‘ λ°λμ returnμ΄ μμ΄μΌ νλ€.
- setterλ 무μΈκ°λ₯Ό νλ‘νΌν°μ ν λΉν λ μ¬μ©νλ―λ‘ λ°λμ λ§€κ°λ³μκ° μμ΄μΌνλ€.
- μ κ·Όμ νλ‘νΌν°λ μΈμ€ν΄μ€μ νλ‘λ‘νμ μ΄ μλλΌ νλ‘ν νμ μ νλ‘νΌν°κ° λλ€.
3) ν΄λμ€ νλ μ μ μ μ
*ν΄λμ€ νλ : ν΄λμ€κ° μμ±ν μΈμ€ν΄μ€μ νλ‘νΌν°
μλ°μ€ν¬λ¦½νΈμ ν΄λμ€ λͺΈμ²΄μλ λ©μλλ§ μ μΈν μ μλ€.
ν΄λμ€ λͺΈμ²΄μ ν΄λμ€ νλλ₯Ό μ μΈνλ©΄ λ¬Έλ² μλ¬(SyntaxError)κ° λ°μνλ€.
β‘οΈ νμ§λ§ μ΅μ λΈλΌμ°μ (Chrome72μ΄μ) λλ Node.js(λ²μ 12μ΄μ)μμλ μ μ λμνλ€.
κ·Έ μ΄μ λ μλ°μ€ν¬λ¦½νΈμμλ μΈμ€ν΄μ€ νλ‘νΌν°λ₯Ό λ§μΉ ν΄λμ€ κΈ°λ° κ°μ²΄μ§ν₯ μΈμ΄μ ν΄λμ€ νλμ²λΌ μ μν μ μλ μλ‘μ΄ νμ€ μ¬μμΈ “Class field declarations“κ° TC39νλ‘μΈμ€μ stage 3(candidate)μ μ μλμ΄ μκΈ° λλ¬Έμ΄λ€.
- ν΄λμ€ νλλ₯Ό μ μνλ κ²½μ° thisμ ν΄λμ€ νλλ₯Ό λ°μΈλ©ν΄μλ μλλ€.
- μ°Έμ‘°νλ κ²½μ° thisλ₯Ό λ°λμ μ¬μ©ν΄μΌ νλ€.
- μ΄κΈ°κ°μ ν λΉνμ§ μμΌλ©΄ undefinedλ₯Ό κ°λλ€.
- μ΄κΈ°νλ constructorμμ ν΄μΌ νλ€.
- ν¨μλ μΌκΈ κ°μ²΄μ΄λ―λ‘ ν¨μλ₯Ό ν΄λμ€ νλμ ν λΉν μ μλ€. μ¦ ν΄λμ€ νλλ₯Ό ν΅ν΄ λ©μλλ₯Ό μ μν μλ μλ€.(μ΄κ²½μ° νλ‘ν νμ λ©μλκ° μλ μΈμ€ν΄μ€ λ©μλκ° λλ€. κΆμ₯νμ§ μμ)
4) private νλ μ μ μ μ
ν΄λμ€ νλλ κΈ°λ³Έμ μΌλ‘ publicνκΈ° λλ¬Έμ μΈλΆμ κ·Έλλ‘ λ ΈμΆλλλ°, TC39 νλ‘μΈμ€μμ private νλλ₯Ό μ μν μ μλ νμ€ μ¬μμ΄ μ μλμ΄ μλ€.
- privateνλλ μ΄λ¦ μμ #μ λΆμ¬μ€λ€. μ°Έμ‘°ν λλ #μ λΆμ¬μ£Όμ΄μΌ νλ€.
- privateνλμ μ§μ μ κ·Όν μ μλ λ°©λ²μ μλ€. λ€λ§ μ κ·Όμ νλ‘νΌν°λ₯Ό ν΅ν΄ κ°μ μ μΌλ‘ κ°λ₯νλ€.
- private νλλ λ°λμ ν΄λμ€ λͺΈμ²΄μ μ μν΄μΌ νλ€. constructorμ μ μνλ©΄ μλ¬κ° λ°μνλ€.
5) static νλ μ μ μ μ
π‘ staticν€μλλ₯Ό μ¬μ©νμ¬ static public field, static private field, static private λ©μλλ₯Ό μ μν μ μλ μλ‘μ΄ νμ€ μ¬μμΈ “Static class features”κ° TC39 νλ‘μΈμ€μ stage 3(candidate)μ μ μλμ΄ μλ€.
class MyMath {
// static public νλ μ μ
static PI = 22 / 7;
// static private νλ μ μ
static #num = 10;
// static λ©μλ
static increment() {
return ++MyMath.#num;
}
}
console.log(MyMath.PI); // 3.142857142857143
console.log(MyMath.increment()); // 11
08. μμμ μν ν΄λμ€ νμ₯
1) ν΄λμ€ μμκ³Ό μμ±μ ν¨μ μμ
νλ‘ν νμ κΈ°λ° μμ vs. μμμ μν ν΄λμ€ νμ₯
νλ‘ν νμ κΈ°λ° μμ : νλ‘ν νμ 체μΈμ ν΅ν΄ λ€λ₯Έ κ°μ²΄μ μμ°μ μμλ°μ
μμμ μν ν΄λμ€ νμ₯ : κΈ°μ‘΄ ν΄λμ€λ₯Ό μμλ°μ μλ‘μ΄ ν΄λμ€λ₯Ό νμ₯(extends)νμ¬ μ μν¨
π‘ μμμ μν ν΄λμ€ νμ₯μ μ½λ μ¬μ¬μ© κ΄μ μμ μ μ©νλ€.
π₯οΈ μμ μ½λ 25-54 <μμμ ν΅ν΄ Animal ν΄λμ€λ₯Ό νμ₯ν Bird ν΄λμ€ κ΅¬ν>
class Animal {
constructor(age, weight) {
this.age = age;
this.weight = weight;
}
eat() { return 'eat'; }
move() { return 'move'; }
}
// μμμ ν΅ν΄ Animal ν΄λμ€λ₯Ό νμ₯ν Bird ν΄λμ€
class Bird extends Animal {
fly() { return 'fly'; }
}
const bird = new Bird(1, 5);
console.log(bird); // Bird {age: 1, weight: 5}
console.log(bird instanceof Bird); // true
console.log(bird instanceof Animal); // true
console.log(bird.eat()); // eat
console.log(bird.move()); // move
console.log(bird.fly()); // fly
2) extends ν€μλ
μμμ ν΅ν΄ ν΄λμ€λ₯Ό νμ₯νλ €λ©΄ extends ν€μλλ₯Ό μ¬μ©νμ¬ μμλ°μ ν΄λμ€λ₯Ό μ μνλ€.
π₯οΈ μμ μ½λ 25-56 (Base ν΄λμ€μ μμμ ν΅ν΄ νμ₯νμ¬ Derived ν΄λμ€λ₯Ό μ μν¨)
// μνΌ(λ² μ΄μ€/λΆλͺ¨)ν΄λμ€
class Base {}
// μλΈ(νμ/μμ)ν΄λμ€
class Derived extends Base {}
β‘οΈ μνΌν΄λμ€μ μλΈν΄λμ€λ μΈμ€ν΄μ€μ νλ‘ν νμ 체μΈλΏ μλλΌ, ν΄λμ€ κ°μ νλ‘ν νμ 체μΈλ μμ±νλ€.
μ΄λ₯Ό ν΅ν΄ νλ‘ν νμ λ©μλ, μ μ λ©μλ λͺ¨λ μμμ΄ κ°λ₯νλ€.
3) λμ μμ
π‘ extendsν€μλλ ν΄λμ€λΏ μλλΌ μμ±μ ν¨μλ₯Ό μμλ°μ ν΄λμ€λ₯Ό νμ₯ν μλ μλ€.
(extendsν€μλ μμλ λ°λμ ν΄λμ€κ° μμΌνλ€.)
π₯οΈ μμ μ½λ 25-57 <μμ±μ ν¨μλ₯Ό μμλ°μ ν΄λμ€λ₯Ό νμ₯>
// μμ±μ ν¨μ
function Base(a) {
this.a = a;
}
// μμ±μ ν¨μλ₯Ό μμλ°λ μλΈν΄λμ€
class Derived extends Base {}
const derived = new Derived(1);
console.log(derived); // Derived {a: 1}
π‘ extendsν€μλ λ€μμλ ν΄λμ€λΏλ§ μλλΌ [[Construct]]λ΄λΆ λ©μλλ₯Ό κ°λ ν¨μ κ°μ²΄λ‘ νκ°λ μ μλ λͺ¨λ ννμμ μ¬μ©ν μ μλ€.(μ΄λ₯Ό ν΅ν΄ λμ μΌλ‘ μμκ°λ₯)
π₯οΈ μμ μ½λ 25-58
function Base1() {}
class Base2 {}
let condition = true;
// 쑰건μ λ°λΌ λμ μΌλ‘ μμ λμμ κ²°μ νλ μλΈν΄λμ€
class Derived extends (condition ? Base1 : Base2) {}
const derived = new Derived();
console.log(derived); // Derived {}
console.log(derived instanceof Base1); // true
console.log(derived instanceof Base2); // false
4) μλΈν΄λμ€μ constructor
μλΈν΄λμ€μμ constructorλ₯Ό μλ΅νλ©΄ μ묡μ μΌλ‘ μ μλλ€.
π₯οΈ μμ μ½λ 25-61
// μνΌν΄λμ€
class Base {}
// μλΈν΄λμ€
class Derived extends Base {}
π₯οΈ μμ μ½λ 25-62
// μνΌν΄λμ€
class Base {
constructor() {}
}
// μλΈν΄λμ€
class Derived extends Base {
constructor() { super(); }
}
const derived = new Derived();
console.log(derived); // Derived {}
β‘οΈ μμ 61μ κ°μ΄ constructorλ₯Ό μλ΅ν κ²½μ°, μμ 62μ κ°μ΄ μ묡μ μΌλ‘ μμ±λλ€.
5) super ν€μλ
- superλ₯Ό νΈμΆ (like ν¨μ) νλ©΄ -> μνΌν΄λμ€μ constructorλ₯Ό νΈμΆ
- superλ₯Ό μ°Έμ‘° (like μλ³μ) νλ©΄ -> μνΌν΄λμ€μ λ©μλλ₯Ό νΈμΆ κ°λ₯
1. super νΈμΆ
π‘ superλ₯Ό νΈμΆνλ©΄ -> μνΌν΄λμ€μ constuctorλ₯Ό νΈμΆνλ€.
π₯οΈ μμ μ½λ 25-63
// μνΌν΄λμ€
class Base {
constructor(a, b) {
this.a = a;
this.b = b;
}
}
// μλΈν΄λμ€
class Derived extends Base {
// λ€μκ³Ό κ°μ΄ μ묡μ μΌλ‘ constructorκ° μ μλλ€.
// constructor(...args) { super(...args); }
}
const derived = new Derived(1, 2);
console.log(derived); // Derived {a: 1, b: 2}
β‘οΈ μνΌν΄λμ€μ constructorλ΄λΆμμ μΆκ°ν νλ‘νΌν°λ₯Ό κ·Έλλ‘ κ°λ μΈμ€ν΄μ€λ₯Ό μμ±νλ€λ©΄
-> μλΈν΄λμ€μ constructorλ₯Ό μλ΅ν μ μλ€.
β‘οΈ new μ°μ°μμ ν¨κ» μλΈν΄λμ€λ₯Ό νΈμΆνλ©΄μ μ λ¬ν μΈμ
-> λͺ¨λ μλΈν΄λμ€μ μ묡μ μΌλ‘ μ μλ constructorμ super νΈμΆμ ν΅ν΄ μνΌν΄λμ€μ constructorμ μ λ¬λ¨.
β οΈ super νΈμΆ μ μ£Όμμ¬ν
1) μλΈν΄λμ€μμ constructorλ₯Ό μλ΅νμ§ μλ κ²½μ° -> μλΈν΄λμ€μ constructorμμλ λ°λμ superλ₯Ό νΈμΆν΄μΌ ν¨.
π₯οΈ μμ μ½λ 25-65
class Base {}
class Derived extends Base {
constructor() {
// ReferenceError: Must call super constructor in derived class before accessing 'this' or returning from derived constructor
console.log('constructor call');
}
}
const derived = new Derived();
β‘οΈ κ·Έλ μ§ μλ κ²½μ° -> ReferenceError λ°μ
2) μλΈν΄λμ€μ constructorμμ superλ₯Ό νΈμΆνκΈ° μ μλ thisμ°Έμ‘° X
π₯οΈ μμ μ½λ 25-66
class Base {}
class Derived extends Base {
constructor() {
// ReferenceError: Must call super constructor in derived class before accessing 'this' or returning from derived constructor
this.a = 1;
super();
}
}
const derived = new Derived(1);
β‘οΈ μΈμ€ν΄μ€ μμ±κ³Ό this λ°μΈλ©μ μνΌν΄λμ€μμ νκΈ° λλ¬Έ!
3) superλ λ°λμ μλΈν΄λμ€μ constructorμμλ§ νΈμΆνλ€.
π₯οΈ μμ μ½λ 25-67
class Base {
constructor() {
super(); // SyntaxError: 'super' keyword unexpected here
}
}
function Foo() {
super(); // SyntaxError: 'super' keyword unexpected here
}
β‘οΈ μλΈν΄λμ€κ° μλ ν΄λμ€μ constructorλ ν¨μμμ superλ₯Ό νΈμΆνλ©΄ μλ¬ λ°μ
2. super μ°Έμ‘°
π‘ λ©μλ λ΄μμ superλ₯Ό μ°Έμ‘°νλ©΄ -> μνΌν΄λμ€μ λ©μλλ₯Ό νΈμΆν μ μλ€.
1) μλΈν΄λμ€μ νλ‘ν νμ λ©μλ λ΄μμ super.sayHiλ μνΌν΄λμ€μ νλ‘ν νμ λ©μλ sayHiλ₯Ό κ°λ¦¬ν¨λ€.
π₯οΈ μμ μ½λ 25-68
// μνΌν΄λμ€
class Base {
constructor(name) {
this.name = name;
}
sayHi() {
return `Hi! ${this.name}`;
}
}
// μλΈν΄λμ€
class Derived extends Base {
sayHi() {
// super.sayHiλ μνΌν΄λμ€μ νλ‘ν νμ
λ©μλλ₯Ό κ°λ¦¬ν¨λ€.
return `${super.sayHi()}. how are you doing?`;
}
}
const derived = new Derived('Lee');
console.log(derived.sayHi()); // Hi! Lee. how are you doing?
β‘οΈ super.syaHiλ Base.prototype.sayHiλ₯Ό κ°λ¦¬ν¨λ€.
μ¦ superλ μμ μ μ°Έμ‘°νκ³ μλ λ©μλκ° λ°μΈλ© λμ΄μλ κ°μ²΄μ νλ‘ν νμ μ κ°λ¦¬ν¨λ€.(Derivedμ sayHi λ©μλκ° λ°μΈλ© λμ΄μλ κ°μ²΄ derivedμ νλ‘ν νμ μΈ Base.prototypeμ κ°λ¦¬ν¨λ€.)
β‘οΈ superμ°Έμ‘°μ λμμ Derivedν΄λμ€μ sayHiλ©μλμ [[HomeObject]]λ΄λΆ μ¬λ‘―μ ν΅ν΄ μμ μ λ°μΈλ©νλ κ°μ²΄λ₯Ό μμμλ€.
μμ μ λ°μΈλ©νλ κ°μ²΄λ₯Ό μλ©΄ νλ‘ν νμ
체μΈμ ν΅ν΄ superλ₯Ό μ°Έμ‘°ν μ μκ²λλ€.
β οΈ μ΄λ, ES6μ λ©μλ μΆμ½ ννμΌλ‘ μ μλ ν¨μλ§ [[HomeObject]]λ₯Ό κ°λλ€λ κ²μ μ£Όμν΄μΌ νλ€.
2) μλΈν΄λμ€μ μ μ λ©μλ λ΄μμ super.sayHiλ μλ²ν΄λμ€μ μ μ λ©μλ sayHiλ₯Ό κ°λ¦¬ν¨λ€.
π₯οΈ μμ μ½λ 25-73
// μνΌν΄λμ€
class Base {
static sayHi() {
return 'Hi!';
}
}
// μλΈν΄λμ€
class Derived extends Base {
static sayHi() {
// super.sayHiλ μνΌν΄λμ€μ μ μ λ©μλλ₯Ό κ°λ¦¬ν¨λ€.
return `${super.sayHi()} how are you doing?`;
}
}
console.log(Derived.sayHi()); // Hi! how are you doing?
6) μμ ν΄λμ€μ μΈμ€ν΄μ€ μμ± κ³Όμ
μμ κ΄κ³μ μλ λ ν΄λμ€κ° μ΄λ»κ² νλ ₯νλ©° μΈμ€ν΄μ€λ₯Ό μμ±νλμ§ μ΄ν΄λ³΄μ.
π₯οΈ μμ μ½λ 25-74
<μ§μ¬κ°νμ μΆμνν Rectangle ν΄λμ€ & μμμ ν΅ν΄ Rectangle ν΄λμ€λ₯Ό νμ₯ν ColorRectangle ν΄λμ€ μ μ>
// μνΌν΄λμ€
class Rectangle {
constructor(width, height) {
this.width = width;
this.height = height;
}
getArea() {
return this.width * this.height;
}
toString() {
return `width = ${this.width}, height = ${this.height}`;
}
}
// μλΈν΄λμ€
class ColorRectangle extends Rectangle {
constructor(width, height, color) {
super(width, height);
this.color = color;
}
// λ©μλ μ€λ²λΌμ΄λ©
toString() {
return super.toString() + `, color = ${this.color}`;
}
}
const colorRectangle = new ColorRectangle(2, 4, 'red');
console.log(colorRectangle); // ColorRectangle {width: 2, height: 4, color: "red"}
// μμμ ν΅ν΄ getArea λ©μλλ₯Ό νΈμΆ
console.log(colorRectangle.getArea()); // 8
// μ€λ²λΌμ΄λ©λ toString λ©μλλ₯Ό νΈμΆ
console.log(colorRectangle.toString()); // width = 2, height = 4, color = red
1. μλΈν΄λμ€μ super νΈμΆ
μλ°μ€ν¬λ¦½νΈ μμ§μ ν΄λμ€λ₯Ό νκ°ν λ μνΌν΄λμ€μ μλΈν΄λμ€λ₯Ό ꡬλΆνκΈ° μν΄ “base” λλ “derive”λ₯Ό κ°μΌλ‘ νλ λ΄λΆ μ¬λ‘―[[ConstructorKind]]λ₯Ό κ°λλ€.
μλΈν΄λμ€λ μμ μ΄ μ§μ μΈμ€ν΄μ€λ₯Ό μμ±νμ§ μκ³ , μνΌν΄λμ€μκ² μΈμ€ν΄μ€ μμ±μ μμνλ€.
μλΈν΄λμ€κ° newμ°μ°μμ ν¨κ» νΈμΆλλ©΄ μλΈν΄λμ€ constructorλ΄λΆμ superν€μλκ° ν¨μμ²λΌ νΈμΆλλ€.(μνΌν΄λμ€κ° νκ°λμ΄ μμ±λ ν¨μ κ°μ²΄μ μ½λκ° μ€νλκΈ° μμνλ€.)
λ§μ½ μλΈν΄λμ€ constructor λ΄λΆμ superνΈμΆμ΄ μμΌλ©΄ μλ¬κ° λ°μνλ€. μ€μ μΈμ€ν΄μ€λ₯Ό μμ±νλ 주체λ μνΌν΄λμ€μ΄λ―λ‘ μνΌν΄λμ€μ constructorλ₯Ό νΈμΆνλ superκ° νΈμΆλμ§ μμΌλ©΄ μΈμ€ν΄μ€λ₯Ό μμ±ν μ μκΈ° λλ¬Έμ΄λ€.
2. μνΌν΄λμ€μ μΈμ€ν΄μ€ μμ±κ³Ό this λ°μΈλ©
μνΌν΄λμ€μ constructorλ΄λΆμ μ½λκ° μ€νλκΈ° μ΄μ μ μ묡μ μΌλ‘ λΉ κ°μ²΄λ₯Ό μμ±νλ€.
μμ±λ λΉ κ°μ²΄λ thisμ λ°μΈλ©λλ€.
π₯οΈ μμ μ½λ 25-75
// μνΌν΄λμ€
class Rectangle {
constructor(width, height) {
// μ묡μ μΌλ‘ λΉ κ°μ²΄, μ¦ μΈμ€ν΄μ€κ° μμ±λκ³ thisμ λ°μΈλ©λλ€.
console.log(this); // ColorRectangle {}
// new μ°μ°μμ ν¨κ» νΈμΆλ ν¨μ, μ¦ new.targetμ ColorRectangleμ΄λ€.
console.log(new.target); // ColorRectangle
...
β‘οΈ μ΄λ μΈμ€ν΄μ€λ μνΌν΄λμ€κ° μμ±ν κ²μ΄λ€.
νμ§λ§ newμ°μ°μμ ν¨κ» νΈμΆλ ν¨μλ₯Ό κ°λ¦¬ν€λ new.targetμ μλΈν΄λμ€λ₯Ό κ°λ¦¬ν¨λ€.
λ°λΌμ μΈμ€ν΄μ€λ new.targetμ΄ κ°λ¦¬ν€λ μλΈν΄λμ€κ° μμ±ν κ²μΌλ‘ μ²λ¦¬λλ€
π₯οΈ μμ μ½λ 25-76
// μνΌν΄λμ€
class Rectangle {
constructor(width, height) {
// μ묡μ μΌλ‘ λΉ κ°μ²΄, μ¦ μΈμ€ν΄μ€κ° μμ±λκ³ thisμ λ°μΈλ©λλ€.
console.log(this); // ColorRectangle {}
// new μ°μ°μμ ν¨κ» νΈμΆλ ν¨μ, μ¦ new.targetμ ColorRectangleμ΄λ€.
console.log(new.target); // ColorRectangle
// μμ±λ μΈμ€ν΄μ€μ νλ‘ν νμ
μΌλ‘ ColorRectangle.prototypeμ΄ μ€μ λλ€.
console.log(Object.getPrototypeOf(this) === ColorRectangle.prototype); // true
console.log(this instanceof ColorRectangle); // true
console.log(this instanceof Rectangle); // true
...
β‘οΈ μμ±λ μΈμ€ν΄μ€λ μλΈν΄λμ€μ prototype νλ‘νΌν°κ° κ°λ¦¬ν€λ κ°μ²΄(ColorRectangle.prototype)μ΄λ€.
3. μνΌν΄λμ€μ μΈμ€ν΄μ€ μ΄κΈ°ν
μνΌν΄λμ€μ constructorκ° μ€νλμ΄ thisμ λ°μΈλ©λμ΄ μλ μΈμ€ν΄μ€λ₯Ό μ΄κΈ°ννλ€.
4. μλΈν΄λμ€ constructorλ‘μ 볡κ·μ this λ°μΈλ©
superμ νΈμΆμ΄ μ’
λ£λκ³ μ μ΄ νλ¦μ΄ μλΈν΄λμ€ construcotrλ‘ λμμ¨λ€.
μ΄λ superκ° λ°νν μΈμ€ν΄μ€κ° thisλ°μΈλ©λλ€.
β οΈ μλΈν΄λμ€λ λ³λμ μΈμ€ν΄μ€λ₯Ό μμ±νμ§ μκ³ superκ° λ°νν μΈμ€ν΄μ€λ₯Ό κ·Έλλ‘ μ¬μ©νλ€.
5. μλΈν΄λμ€μ μΈμ€ν΄μ€ μ΄κΈ°ν
superνΈμΆ μ΄ν μλΈν΄λμ€μ constructorμ κΈ°μ λμ΄ μλ μΈμ€ν΄μ€ μ΄κΈ°νκ° μ€νλλ€.
6. μΈμ€ν΄μ€ λ°ν
ν΄λμ€μ λͺ¨λ μ²λ¦¬κ° λλλ©΄ μμ±λ μΈμ€ν΄μ€κ° λ°μΈλ©λ thisκ° μ묡μ μΌλ‘ λ°νλλ€.
7) νμ€ λΉνΈμΈ μμ±μ ν¨μ νμ₯
extends ν€μλ λ€μμλ ν΄λμ€λΏλ§μ΄ μλλΌ [[Construct]]λ΄λΆ λ©μλλ₯Ό κ°λ ν¨μ κ°μ²΄λ‘ νκ°λ μ μλ λͺ¨λ ννμμ μ¬μ©ν μ μλ€.
String, Number, Arrayκ°μ νμ€ λΉνΈμΈ κ°μ²΄λ [[Construct]]λ΄λΆ λ©μλλ₯Ό κ°λ μμ±μ ν¨μμ΄λ―λ‘ extendsν€μλλ₯Ό μ¬μ©νμ¬ νμ₯ν μ μλ€.
π₯οΈ μμ μ½λ 25-80
// Array μμ±μ ν¨μλ₯Ό μμλ°μ νμ₯ν MyArray
class MyArray extends Array {
// μ€λ³΅λ λ°°μ΄ μμλ₯Ό μ κ±°νκ³ λ°ννλ€: [1, 1, 2, 3] => [1, 2, 3]
uniq() {
return this.filter((v, i, self) => self.indexOf(v) === i);
}
// λͺ¨λ λ°°μ΄ μμμ νκ· μ ꡬνλ€: [1, 2, 3] => 2
average() {
return this.reduce((pre, cur) => pre + cur, 0) / this.length;
}
}
const myArray = new MyArray(1, 1, 2, 3);
console.log(myArray); // MyArray(4) [1, 1, 2, 3]
// MyArray.prototype.uniq νΈμΆ
console.log(myArray.uniq()); // MyArray(3) [1, 2, 3]
// MyArray.prototype.average νΈμΆ
console.log(myArray.average()); // 1.75
β‘οΈ Array μμ±μ ν¨μλ₯Ό μμλ°μ νμ₯ν MyArray ν΄λμ€κ° μμ±ν μΈμ€ν΄μ€λ, Array.prototypeκ³Ό MyArray.prototypeμ λͺ¨λ λ©μλλ₯Ό μ¬μ©ν μ μλ€.
π μ°Έκ³ μλ£