Class
JSμμλ ν΄λμ€λ₯Ό μ¬μ©νμ¬ κ°μ²΄ μ§ν₯μ μΌλ‘ νλ‘κ·Έλλ°μ΄ κ°λ₯νλ€.
ννμ
class λ₯Ό μ΄μ©ν΄ μ μΈνκ³ newλ₯Ό μ΄μ©ν΄ μμ±
class User {
constructor(name) {
//μμ±μ
this.name = name;
}
sayHi() {
alert(this.name);
}
}
let user = new User('John');
user.sayHi();
alert(typeof User); // function
alert(User === User.prototype.constructor); // true
alert(User.prototype.sayHi); // alert(this.name);
alert(Object.getOwnPropertyNames(User.prototype)); // constructor, sayHiκΈ°λͺ ννμ
ν΄λμ€ λμ μμ±
μ리
μμ μ½λλ₯Ό μλ‘ UserλΌλ ν¨μλ₯Ό λ§λ€κ³ ν¨μλ μμ±μconstructorλ‘ λΆν° μμ±νκ³ ν΄λμ€ λ΄μ λ©μλλ€μ User.prototypeμ μ μ₯νλ€
μΌλ°μ μΈ μμ±μ ν¨μμ λ€λ₯Έμ
classλ‘ λ§λ ν¨μμ νΉμ λ΄λΆ νλ‘νΌν°μΈ
[[FunctionKind]]:"classConstructor"κ° μ‘΄μ¬νλ€. λ°λΌμnewλ₯Ό λΆμ΄μ§ μκ³User();μ κ°μ΄ κ·Έλ₯ νΈμΆνκ² λλ€λ©΄ μλͺ» μ¬μ©λκ²μ κ°μ§νκ³ μλ¬λ₯Ό λ°μμν¨λ€.prototypeμ λ©μλ μ 체μ
enumerableνλκ·Έλ falseλ‘ ν΄λμ€ λ©μλλ μ΄κ±°κ° λΆκ°λ₯ νλ€.νμ
use strictλͺ¨λλ‘ μ€νλμ΄ ν΄λμ€ μμ±μ μμ μ½λ μ 체μ μλμΌλ‘ μ격λͺ¨λκ° μ μ©λλ€.
getter/setter
리ν°λ΄μ μ¬μ©ν΄ λ§λ κ°μ²΄μ²λΌ getter/setterλ₯Ό λ§λ€ μ μλ€.
ν΄λμ€ νλ
νλ‘νΌν°μ΄λ¦ = κ° μΌλ‘ νλλ₯Ό μΆκ°κ° κ°λ₯νλ©°, Obj.prototypeμ΄ μλ κ°λ³ κ°μ²΄μλ§ ν΄λμ€ νλκ° μ€μ λλ€.
Last updated