μΈμ€ν΄μ€ νλ‘νΌν°
μΈμ€ν΄μ€ νλ‘νΌν°λ constructor
λ΄λΆμμ μ μν΄μΌ νλ€.
class Person {
constructor(name) {
// μΈμ€ν΄μ€ νλ‘νΌν°
this.name = name;
}
}
const me = new Person('Jung');
console.log(me); // Person {name: "Jung"}
constructor
λ΄λΆ μ½λκ° μ€νλκΈ° μ΄μ μ constructor
λ΄λΆμ this
μλ μ΄λ―Έ ν΄λμ€κ° μ묡μ μΌλ‘ μμ±ν μΈμ€ν΄μ€μΈ λΉ κ°μ²΄κ° λ°μΈλ©λμ΄ μλ€.
μμ±μ ν¨μμμ μμ±μ ν¨μκ° μμ±ν μΈμ€ν΄μ€μ νλ‘νΌν°λ₯Ό μ μνλ κ²κ³Ό λ§μ°¬κ°μ§λ‘, constructor
λ΄λΆμμ this
μ μΈμ€ν΄μ€ νλ‘νΌν°λ₯Ό μΆκ°νλ€. μ΄λ‘μ¨ ν΄λμ€κ° μ묡μ μΌλ‘ μμ±ν λΉ κ°μ²΄μΈ μΈμ€ν΄μ€μ νλ‘νΌν°κ° μΆκ°λμ΄ μΈμ€ν΄μ€κ° μ΄κΈ°νλλ€.
class Person {
constructor(name) {
this.name = name; // name νλ‘νΌν°λ publicνλ€.
}
}
const me = new Person('Jung');
console.log(me.name); // Jung
constructor
λ΄λΆμμ this
μ μΆκ°ν νλ‘νΌν°λ μΈμ λ ν΄λμ€κ° μμ±ν μΈμ€ν΄μ€μ νλ‘νΌν°κ° λλ€.
ES6μ ν΄λμ€λ λ€λ₯Έ κ°μ²΄μ§ν₯ μΈμ΄μ²λΌ private
, public
, protected
ν€μλμ κ°μ μ κ·Ό μ νμλ₯Ό μ§μνμ§ μλλ€. νμ§λ§ private νλκ° μ μμΌλ‘ μ±νλλ©΄μ μΈμ€ν΄μ€ νλ‘νΌν°λ public νΉμ privateμΌ μ μκ² λλ€.
μ κ·Όμ νλ‘νΌν°
μ κ·Όμ νλ‘νΌν°λ μ체μ μΌλ‘λ κ°([[Value]]
λ΄λΆ μ¬λ‘―)μ κ°μ§ μκ³ λ€λ₯Έ λ°μ΄ν° νλ‘νΌν°μ κ°μ μ½κ±°λ μ μ₯ν λ μ¬μ©νλ μ κ·Όμ ν¨μλ‘ κ΅¬μ±λ νλ‘νΌν°λ€.
μ κ·Όμ ν¨μμλ getter ν¨μμ setter ν¨μκ° μλ€. getterλ μΈμ€ν΄μ€ νλ‘νΌν°μ μ κ·Όν λλ§λ€ νλ‘νΌν°μ κ°μ μ‘°μνκ±°λ λ³λμ νμκ° νμν λ μ¬μ©νλ€. getterλ λ©μλ μ΄λ¦ μμ get
ν€μλλ₯Ό μ¬μ©ν΄ μ μνλ€. setterλ μΈμ€ν΄μ€ νλ‘νΌν°μ κ°μ ν λΉν λλ§λ€ νλ‘νΌν°μ κ°μ μ‘°μνκ±°λ λ³λμ νμκ° νμν λ μ¬μ©νλ€. setterλ λ©μλ μ΄λ¦ μμ set
ν€μλλ₯Ό μ¬μ©ν΄ μ μνλ€.
getterμ setter μ΄λ¦μ μΈμ€ν΄μ€ νλ‘νΌν°μ²λΌ μ¬μ©λλ€. μ¦, getterλ νΈμΆνλ κ²μ΄ μλλΌ νλ‘νΌν°μ²λΌ μ°Έμ‘°νλ νμμΌλ‘ μ¬μ©νλ©°, μ°Έμ‘° μ λ΄λΆμ μΌλ‘ getterκ° νΈμΆλλ€. setterλ νΈμΆνλ κ²μ΄ μλλΌ νλ‘νΌν°μ²λΌ κ°μ ν λΉνλ νμμΌλ‘ μ¬μ©νλ©°, ν λΉμ λ΄λΆμ μΌλ‘ setterκ° νΈμΆλλ€.
getterλ μ΄λ¦ κ·Έλλ‘ λ¬΄μΈκ°λ₯Ό μ·¨λν λ μ¬μ©νλ―λ‘ λ°λμ 무μΈκ°λ₯Ό λ°νν΄μΌ νκ³ setterλ 무μΈκ°λ₯Ό νλ‘νΌν°μ ν λΉν΄μΌ ν λ μ¬μ©νλ―λ‘ λ°λμ λ§€κ°λ³μκ° μμ΄μΌ νλ€. setterλ λ¨ νλμ κ°λ§ ν λΉλ°κΈ° λλ¬Έμ λ¨ νλμ λ§€κ°λ³μλ§ μ μΈν μ μλ€.
const person = {
firstName: 'ek',
lastName: 'jung',
// fullNameμ μ κ·Όμ ν¨μλ‘ κ΅¬μ±λ μ κ·Όμ νλ‘νΌν°
get fullName() {
return `${this.firstName} ${this.lastName}`;
},
set fullName(name) {
[this.firstName, this.lastName] = name.split(' ');
}
};
console.log(`${person.firstName} ${person.lastName}`); // ek jung
person.fullName = 'ek jung';
console.log(person); // {firstName: "ek", lastName: "jung"}
console.log(person.fullNmae); // ek jung
// μ κ·Όμ νλ‘νΌν°λ get, set, enumerable, configurable νλ‘νΌν° μ΄νΈλ¦¬λ·°νΈλ₯Ό κ°λλ€.
console.log(Object.getOwnPropertyDescriptor(person, 'fullName');
// {get: f, set: f, enumerable: true, configurable: true}
μ κ·Όμ νλ‘νΌν°λ ν΄λμ€μμλ μ¬μ©ν μ μλ€. μ μμ λ₯Ό ν΄λμ€λ‘ νννλ©΄ μλμ κ°λ€.
class Person {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
get fullName() {
return `${this.firstName} ${this.lastName}`;
}
set fullName(name) {
[this.firstName, this.lastName] = name.split(' ');
}
}
const me = new Person('ek', 'jung');
console.log(`${me.firstName} ${me.lastName}`); // ek jung
me.fullName = 'ek jung';
console.log(me); // {firstName: "ek", lastName: "jung"}
console.log(me.fullNmae); // ek jung
console.log(Object.getOwnPropertyDescriptor(person.prototype, 'fullName');
// {get: f, set: f, enumerable: true, configurable: true}
ν΄λμ€μ λ©μλλ κΈ°λ³Έμ μΌλ‘ νλ‘ν νμ λ©μλκ° λλ―λ‘, ν΄λμ€μ μ κ·Όμ νλ‘νΌν° λν μΈμ€ν΄μ€ νλ‘νΌν°κ° μλ νλ‘ν νμ μ νλ‘νΌν°κ° λλ€.
Object.getOwnPropertyNames(me); // ["firstName", "lastName"]
Object.getOwnPropertyNames(Object.getPrototypeOf(me)); // ["constructor", "fullName"]
ν΄λμ€ νλ μ μ μ μ
ν΄λμ€ νλλ?
ν΄λμ€ νλλ ν΄λμ€ κΈ°λ° κ°μ²΄μ§ν₯ μΈμ΄μμ ν΄λμ€κ° μμ±ν μΈμ€ν΄μ€μ νλ‘νΌν°λ₯Ό κ°λ¦¬ν€λ μ©μ΄λ€. ν΄λμ€ κΈ°λ° κ°μ²΄μ§ν₯ μΈμ΄μΈ μλ°μ ν΄λμ€ μ μλ₯Ό μ΄ν΄λ³΄λ©΄, μλ°μ ν΄λμ€ νλλ λ§μΉ ν΄λμ€ λ΄λΆμμ λ³μμ²λΌ μ¬μ©λλ€.
// μλ°μ ν΄λμ€ μ μ
public class Person {
// β ν΄λμ€ νλ μ μ: ν΄λμ€ νλλ ν΄λμ€ λͺΈμ²΄μ this μμ΄ μ μΈ
private String firstName = "";
private String lastName = "";
// μμ±μ
Person(String firstName, String lastName) {
// β’ thisλ μΈμ λ ν΄λμ€κ° μμ±ν μΈμ€ν΄μ€λ₯Ό κ°λ¦¬ν΄
this.firstName = firstName;
this.lastName = lastName;
}
public String getFullName() {
// β‘ ν΄λμ€ νλ μ°Έμ‘°: this μμ΄λ ν΄λμ€ νλ μ°Έμ‘° κ°λ₯
return firstName + " " + lastName;
}
}
μλ°μ€ν¬λ¦½νΈμ ν΄λμ€μμ μΈμ€ν΄μ€ νλ‘νΌν°λ₯Ό μ μΈνκ³ μ΄κΈ°ννλ €λ©΄ λ°λμ constructor
λ΄λΆμμ this
μ νλ‘νΌν°λ₯Ό μΆκ°ν΄μΌ νλ€. νμ§λ§ μλ°μ ν΄λμ€μμλ μ μμ β κ³Ό κ°μ΄ ν΄λμ€ νλλ₯Ό λ§μΉ λ³μμ²λΌ ν΄λμ€ λͺΈμ²΄μ this
μμ΄ μ μΈνλ€.
μλ°μ€ν¬λ¦½νΈμ ν΄λμ€μμ μΈμ€ν΄μ€ νλ‘νΌν°λ₯Ό μ°Έμ‘°νλ €λ©΄ λ°λμ this
λ₯Ό μ¬μ©νμ¬ μ°Έμ‘°ν΄μΌ νλ€. νμ§λ§ μλ°μ ν΄λμ€μμλ μ μμ β‘μ κ°μ΄ this
λ₯Ό μλ΅ν΄λ ν΄λμ€ νλλ₯Ό μ°Έμ‘°ν μ μλ€.
ν΄λμ€ κΈ°λ° κ°μ²΄μ§ν₯ μΈμ΄μ this
λ μΈμ λ ν΄λμ€κ° μμ±ν μΈμ€ν΄μ€λ₯Ό κ°λ¦¬ν€κ³ μμ΄μ, μ μμ β’κ³Ό κ°μ΄ this
λ μ£Όλ‘ ν΄λμ€ νλκ° μμ±μ λλ λ©μλμ λ§€κ°λ³μ μ΄λ¦κ³Ό λμΌν λ ν΄λμ€ νλμμ λͺ
νν νκΈ° μν΄ μ¬μ©νλ€.
μλ°μ€ν¬λ¦½νΈμμ ν΄λμ€ νλ
μλ°μ€ν¬λ¦½νΈμ ν΄λμ€ λͺΈμ²΄μλ μλ λ©μλλ§ μ μΈν μ μλ€. λ°λΌμ μ΄μ μλ ν΄λμ€ λͺΈμ²΄μ μλ°μ μ μ¬νκ² ν΄λμ€ νλλ₯Ό μ μΈνλ©΄ λ¬Έλ² μλ¬κ° λ°μνμλ€. νμ§λ§ μλ°μ€ν¬λ¦½νΈμμλ μΈμ€ν΄μ€ νλ‘νΌν°λ₯Ό λ§μΉ ν΄λμ€ κΈ°λ° κ°μ²΄μ§ν₯ μΈμ΄μ ν΄λμ€ νλμ²λΌ μ μν μ μλ μλ‘μ΄ νμ€ μ¬μμΈ "Class field declarations"κ° μ μλμ΄ ECMAScript 2022λΆν° μ μ μ±νλ μνλ€. λ°λΌμ μ΄μ λ μλμ κ°μ΄ ν΄λμ€ νλλ₯Ό ν΄λμ€ λͺΈμ²΄μ μ μν μ μλ€.
class Person {
name = 'Jung';
}
const me = new Person('Jung');
ν΄λμ€ νλ μ£Όμ μ¬ν
ν΄λμ€ λͺΈμ²΄μμ ν΄λμ€ νλλ₯Ό μ μν λ this
μ ν΄λμ€ νλλ₯Ό λ°μΈλ©ν΄μλ μλλ€. this
λ ν΄λμ€μ constructor
μ λ©μλ λ΄μμλ§ μ ν¨νλ€.
class Person {
this.name = ''; // SyntaxError: Unexpected token '.'
}
ν΄λμ€ νλλ₯Ό μ°Έμ‘°νλ κ²½μ° μλ°μ κ°μ ν΄λμ€ κΈ°λ° κ°μ²΄μ§ν₯ μΈμ΄μμλ this
λ₯Ό μλ΅ν μ μμ§λ§, μλ°μ€ν¬λ¦½νΈμμλ ν΄λμ€ νλ μ°Έμ‘° μμλ this
λ₯Ό λ°λμ μ¬μ©ν΄μΌ νλ€.
class Person {
// ν΄λμ€ νλ
name = 'Jung';
constructor() {
console.log(name); // ReferenceError: name is not defined
}
}
new Person();
ν΄λμ€ νλμ μ΄κΈ°κ°μ ν λΉνμ§ μμΌλ©΄ undefined
λ₯Ό κ°λλ€.
class Person {
name;
}
const me = new Person();
console.log(me); // Person {name: undefined}
μΈμ€ν΄μ€λ₯Ό μμ±ν λ μΈλΆμ μ΄κΈ°κ°μΌλ‘ ν΄λμ€ νλλ₯Ό μ΄κΈ°νν΄μΌ ν νμκ° μλ€λ©΄ constructor
μμ ν΄λμ€ νλλ₯Ό μ΄κΈ°νν΄μΌ νλ€.
class Person {
name;
constructor(name) {
this.name = name;
}
}
const me = new Person('Jung');
console.log(me); // Person {name: "Jung"}
μ μμ μ²λΌ μΈμ€ν΄μ€λ₯Ό μμ±ν λ ν΄λμ€ νλλ₯Ό μ΄κΈ°νν νμκ° μλ€λ©΄ constructor
λ°μμ ν΄λμ€ νλλ₯Ό μ μν νμκ° μλ€. ν΄λμ€ νλλ₯Ό μ΄κΈ°νν νμκ° μλ€λ©΄ μ΄μ°¨νΌ constructor
λ΄λΆμμ ν΄λμ€ νλλ₯Ό μ°Έμ‘°νμ¬ μ΄κΈ°κ°μ ν λΉν΄μΌ νλ€. μ΄λ, this
, μ¦ ν΄λμ€κ° μμ±ν μΈμ€ν΄μ€μ ν΄λμ€ νλμ ν΄λΉνλ νλ‘νΌν°κ° μλ€λ©΄ μλ μΆκ°λκΈ° λλ¬Έμ΄λ€.
class Person {
constructor(name) {
this.name = name;
}
}
const me = new Person('Jung');
console.log(me); // Person {name: "Jung"}
ν¨μλ μΌκΈ κ°μ²΄μ΄λ―λ‘ ν¨μλ₯Ό ν΄λμ€ νλμ ν λΉν μ μλ€. λ°λΌμ ν΄λμ€ νλλ₯Ό ν΅ν΄ λ©μλλ₯Ό μ μν μλ μλ€.
class Person {
name = 'Jung';
getName = function () {
return this.name;
}
// νμ΄ν ν¨μλ‘λ μ μ κ°λ₯
// getName = () => this.name;
}
const me = new Person();
console.log(me); // Person {name: "Jung", getName: f}
console.log(me.getName()); // Jung
μ΄μ²λΌ ν΄λμ€ νλμ ν¨μλ₯Ό ν λΉνλ κ²½μ°, μ΄ ν¨μλ νλ‘ν νμ
λ©μλκ° μλ μΈμ€ν΄μ€ λ©μλκ° λλ€. λͺ¨λ ν΄λμ€ νλλ μΈμ€ν΄μ€ νλ‘νΌν°κ° λκΈ° λλ¬Έμ΄λ€. λ°λΌμ this
λ°μΈλ©μ΄ κΌ νμνκ±°λ μ€μ λ‘ μΈμ€ν΄μ€ λ³λ‘ ꡬνμ΄ λ€λ₯Έ ν¨μμΌ κ²½μ°λ₯Ό μ μΈνκ³ ν΄λμ€ νλμ ν¨μλ₯Ό ν λΉνλ κ²μ κΆμ₯λμ§ μλλ€.
private νλ μ μ μ μ
μλ°μ€ν¬λ¦½νΈλ μΊ‘μνλ₯Ό μμ νκ² μ§μνμ§ μλλ€. ES6μ ν΄λμ€λ μμ±μ ν¨μμ λ§μ°¬κ°μ§λ‘ λ€λ₯Έ ν΄λμ€ κΈ°λ° κ°μ²΄μ§ν₯ μΈμ΄μμ μ§μνλ private
, public
, protected
ν€μλμ κ°μ μ κ·Ό μ νμλ₯Ό μ§μνμ§ μλλ€. λ°λΌμ μΈμ€ν΄μ€ νλ‘νΌν°λ μΈμ€ν΄μ€λ₯Ό ν΅ν΄ ν΄λμ€ μΈλΆμμ μΈμ λ μ°Έμ‘°ν μ μλ€. μ¦, μΈμ λ public
μ΄μλ€. νμ§λ§ private
νλλ₯Ό μ μν μ μλ μλ‘μ΄ νμ€ μ¬μμ΄ μ μλμ΄ ECMAScript 2022λΆν° μ μμΌλ‘ μ±νλμλ€.
private
νλμ μ λμλ #
μ λΆμ¬μ€λ€. private
νλλ₯Ό μ°Έμ‘°ν λλ #
μ λΆμ¬μ£Όμ΄μΌ νλ€.
class Person {
#name = '';
constructor(name) {
this.#name = name;
}
}
const me = new Person('Jung');
console.log(me.#name); // SyntaxError: Private field '#name' must be declared in an enclosing class
public
νλλ μ΄λμλ μ°Έμ‘°ν μ μμ§λ§ private
νλλ ν΄λμ€ λ΄λΆμμλ§ μ°Έμ‘°ν μ μλ€. λ°λΌμ ν΄λμ€ μΈλΆμμ private
νλμ μ§μ μ κ·Όν μ μλ λ°©λ²μ μλ€. λ€λ§ μ κ·Όμ νλ‘νΌν°λ₯Ό ν΅ν΄ κ°μ μ μΌλ‘ μ κ·Όνλ λ°©λ²μ μ ν¨νλ€.
class Person {
#name = '';
constructor(name) {
this.#name = name;
}
get name() {
return this.#name.trim();
}
}
const me = new Person(' Jung ');
console.log(me.name); // Jung
private
νλλ λ°λμ λͺΈμ²΄μ μ μν΄μΌ νλ€. λ§μ½ private
νλλ₯Ό μ§μ constructor
μ μ μνλ©΄ μλ¬κ° λ°μνλ€.
class Person {
constructor(name) {
this.#name = name;
// SyntaxError: Private field '#name' must be declared in an enclosing class
}
}
static νλ μ μ μ μ
ν΄λμ€μμλ static
ν€μλλ₯Ό μ¬μ©νμ¬ μ μ λ©μλλ₯Ό μ μν μ μμ§λ§, μ μ νλλ μ μν μ μμλ€. νμ§λ§ static public
νλ, static private
νλ, static private
λ©μλλ₯Ό μ μν μ μλ μλ‘μ΄ νμ€ μ¬μμΈ "Static class features"κ° μ μλμ΄ ESCAScript 2022λΆν° μ μμΌλ‘ μ±νλμλ€.
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
μ°Έκ³
- μ μ
- μ΄μ λͺ¨
- μΆν
- μν€λΆμ€
- μΆνμΌ
- 2020.09.25