μλ° μ€ν¬λ¦½νΈλ μλ ν΄λμ€λ μμκ³ νλ‘ν νμ μμ€ν κ³Ό μμ±μ ν¨μλ§ μμλ€.
μμ±μ μ μλ νμλ μλμ§λ§ λκ° ν΄λμ€λ₯Ό μ μν λ μ¬μ©νλ€.
constructorλΌκ³ μ μν΄μΌ μλμΌλ‘ νΈμΆλλ€.
class Player{
//μμ±μ
constructor(first, last){
this.first = first;
this.last = last;
console.log("IN CONSTRUCTOR!")
}
taunt(){
console.log("YEAH!!")
}
}
const player1 = new Player("blue", "steele");
console.log(player1.taunt()); // IN CONSTRUCTOR! YEAH!!
console.log(player1.first); // blue
console.log(player1.last); // steele
ν΄λμ€ νλλ νλμ νλ‘νΌν°λ₯Ό λΉ λ₯΄κ² μ μνκ² ν΄μ£Όλ ꡬ문μΌλ‘μ ν΄λμ€ μμμ μμ λ‘κ² λ°κΏκ°λ©° μ¬μ©ν μ μλ€.
class Player{
score = 0; // class field
numLives = 10; // class field
//μμ±μ
constructor(first, last){
this.first = first;
this.last = last;
console.log("IN CONSTRUCTOR!");
}
taunt(){
console.log("YEAH!!");
}
loseLife(){
this.numLives -= 1;
}
}
const player1 = new Player("blue", "steele");
console.log(player1.numLives); // 10
plyaer1.loseLife();
console.log(player1.numLives); // 9
νλΌμ΄λΉ νλλ₯Ό μ€μ νκΈ° μν΄μλ νλ‘νΌν°λ νλ μμ # κΈ°νΈλ₯Ό μΆκ°νλ©΄ λλ€.
νλΌμ΄λΉ νλλ ν΄λμ€ μμμλ§ μ¬μ©κ°λ₯νλ©° λ°μμλ μ κ·Όν μ μλ€.
class Player{
#score = 0; // private field Player ν΄λμ€ μμμλ§ μ¬μ©κ°λ₯
numLives = 10;
//μμ±μ
constructor(first, last){
this.first = first;
this.last = last;
console.log("IN CONSTRUCTOR!");
}
getScore(){
return this.#score;
}
setScore(newScore){
this.#score = newScore;
}
}
const player1 = new Player("blue", "steele");
console.log(player1.#score); // error
console.log(player1.getScore); // 0
console.log(player1.setScore(28));
console.log(player1.getScore); // 28
class Player{
#score = 0; // private field Player ν΄λμ€ μμμλ§ μ¬μ©κ°λ₯
numLives = 10;
//μμ±μ
constructor(first, last){
this.first = first;
this.last = last;
console.log("IN CONSTRUCTOR!");
}
get fullName(){ // getters
return `${this.first} ${this.last}`
}
get score(){ // getters
return this.#score;
}
set score(newScore){ // setters
if(newScore < 0){
throw new Error("Score must be positive!");
}
this.#score = newScore;
}
}
const player1 = new Player("blue", "steele");
console.log(player1.score); // 0
console.log(player1.score(5));
console.log(player1.score); // 5
νΉμ μΈμ€ν΄μ€μ κ΄λ ¨ μμΌλ©΄μ ν΄λμ€ μ체μ μ°κ΄λ κΈ°λ₯μ ν΄λμ€λ‘ κ·Έλ£Ήνν μ μκ²ν΄μ€λ€.
μ¦, staticμ νλ‘νΌν°λ λ©μλκ° ν΄λμ€μλ§ μλ€κ³ μλ €μ£Όλ μν μ νλ€.
μ£Όλ‘ μμ± λ©μλ νΉμ μλ‘μ΄ μΈμ€ν΄μ€λ μ¬λ¬ μΈμ€ν΄μ€λ₯Ό μμ±νλ ν¬νΌ λ±μ λ§λ€λ μ°μΈλ€.
class Player{
static description = "Player In Our Game";
#score = 0; // private field Player ν΄λμ€ μμμλ§ μ¬μ©κ°λ₯
numLives = 10;
//μμ±μ
constructor(first, last){
this.first = first;
this.last = last;
console.log("IN CONSTRUCTOR!");
}
static randomPlayer(){
return new Player("Andy", "Samberg");
}
}
const player1 = new Player("blue", "steele");
console.log(player1.description); // undefined
console.log(Player.description); // Player In Our Game
extendsλ‘ κΈ°λ³Έ ν΄λμ€μ λΆλͺ¨ ν΄λμ€λ₯Ό νμ₯ ν μ μλ€.
μμ±μκ° μ¬λΏμ΄λΌλ©΄ superλ‘ λΆλͺ¨ μμ±μλ₯Ό λ¨Όμ νΈμΆν λ€μ μνλ κ°μ μ λ¬ν΄μΌ μμ ν΄λμ€μμ μνλ μΌμ ν μ μλ€.
class Player{
static description = "Player In Our Game";
#score = 0; // private field Player ν΄λμ€ μμμλ§ μ¬μ©κ°λ₯
numLives = 10;
//μμ±μ
constructor(first, last){
this.first = first;
this.last = last;
console.log("IN CONSTRUCTOR!")
}
static randomPlayer(){
return new Player("Andy", "Samberg");
}
taunt(){
console.log("YEAH!!")
}
}
class AdminPlayer extends Player{ // ν΄λμ€ νμ₯
constructor(first, last, powers){
super(first,last) // Player constructorλ₯Ό μλ―Έ
this.powers = powers
}
isAdmin = true;
}
const admin = new AdminPlayer("admin", "mCadmin", ["delete", "restore"]);