Section 12: Practice: Object-oriented Programming - Time to Practice!
The goals
๐ช๐ปPractice Object-oriented Programming
static
The static keyword defines a static method or property for a class, or a class static initialization block (see the link for more information about this usage). Neither static methods nor static properties can be called on instances of the class. Instead, they're called on the class itself.
Static methods are often utility functions, such as functions to create or clone objects, whereas static properties are useful for caches, fixed-configuration, or any other data you don't need to be replicated across instances.
prototype์ด ์๋ ํด๋์ค ํจ์ ์์ฒด์ ๋ฉ์๋๋ฅผ ์ค์ ํ ์ ์๋ค
์ด๋ฐ ๋ฉ์๋๋ฅผ ์ ์ (static) ๋ฉ์๋๋ผ๊ณ ๋ถ๋ฅธ๋ค
class Person {
static staticMethod() {
console.log("static");
}
}
Person.staticMethod(); // static
์ ์ ๋ฉ์๋๋ ๋ฉ์๋๋ฅผ ํ๋กํผํฐ ํํ๋ก ์ง์ ํ ๋นํ๋ ๊ฒ๊ณผ ๋์ผํ ์ผ์ ํ๋ค
class Person { }
Person.staticMethod = function() {
console.log("static");
}
Person.staticMethod(); // static
Person.staticMethod()๊ฐ ํธ์ถ๋ ๋ this์ ๊ฐ์ ํด๋์ค ์์ฑ์์ธ User ์์ฒด๊ฐ ๋๋ค (์ ์ ๊ฐ์ฒด)
์ ์ ๋ฉ์๋๋ ์ด๋ค ํน์ ํ ๊ฐ์ฒด๊ฐ ์๋ ํด๋์ค์ ์ํ ํจ์๋ฅผ ๊ตฌํํ ๋ ์ฃผ๋ก ์ฌ์ฉ๋๋ค
class Posting {
constructor(title, date) {
this.title = title;
this.date = date;
}
static compareDate(postingA, postingB) {
return postingA.date - postingB.date;
}
}
let postings = [
new Posting("์ ์ ๋ฉ์๋", new Date(2022, 12, 22)),
new Posting("์๋ํ", new Date(2022, 11, 28)),
new Posting("๊ฐ์ฒด์งํฅ", new Date(2022, 05, 10)),
];
posting.sort(Posting.compareDate);
conole.log(postins[0].title); // ๊ฐ์ฒด์งํฅ
Posting.compareDate๋ posting์ ๋น๊ตํด์ฃผ๋ ์๋จ์ผ๋ก ๊ธ ์ ์ฒด๋ฅผ ์์์ ๋ฐ๋ก๋ณด๋ฉฐ ๋น๊ต๋ฅผ ์ํํ๋ค
Posting.compareDate๋ ๊ธ ํ๋์ ๋ฉ์๋๊ฐ ์๋ ํด๋์ค์ ๋ฉ์๋์ฌ์ผ ํ๋ ์ด์ ์ด๋ค
ํฉํ ๋ฆฌ ๋ฉ์๋๋ฅผ ๊ตฌํํ ์ฝ๋์ด๋ค
class Posting {
constructor(title, date) {
this.title = title;
this.date = date;
}
static createToday() {
// this๋ Posting์ด๋ค
return new this("Monday", new Date());
}
}
let posting = Posting.createToday();
console.log(posting.title); // Monday
์ด์ Monday๋ผ๋ ๋ฌธ์๊ฐ ํ์ํ ๋๋ง๋ค Posting.createToday()๋ฅผ ํธ์ถํ๋ฉด ๋๋ค
์ฌ๊ธฐ์๋ ๋ง์ฐฌ๊ฐ์ง๋ก Posting.createToday()๋ posting ๋ฉ์๋๊ฐ ์๋ ์ ์ฒด ํด๋์ค์ ๋ฉ์๋์ด๋ค
์ ์ ๋ฉ์๋๋ ํญ๋ชฉ ๊ฒ์, ์ ์ฅ, ์ญ์ , ์์ ๋ฑ ๋ฐ์ดํฐ ๋ฒ ์ด์ค ๊ด๋ จ ํด๋์ค์์๋ ์ฌ์ฉํ ์ ์๋ค
Posting.remove({ id: 1001 })
์ ์ ํ๋กํผํฐ
์ ์ ํ๋กํผํฐ๋ ์ผ๋ฐ ํด๋์ค ํ๋กํผํฐ์ ์ ์ฌํ๊ฒ ์์ static์ด ๋ถ๋๋ค
class Posting {
static postA = "ํจ์ํ ํ๋ก๊ทธ๋๋ฐ";
}
console.log(Posting.postA); // ํจ์ํ ํ๋ก๊ทธ๋๋ฐ
Posting์ ํ๋กํผํฐ๋ฅผ ์ง์ ํ ๋นํ๋ ๊ฒ๊ณผ ๋์ผํ๊ฒ ๋์ํ๋ค
Posting.postA = "ํจ์ํ ํ๋ก๊ทธ๋๋ฐ";
์ ์ ํ๋กํผํฐ์ ๋ฉ์๋ ์์
์ ์ ํ๋กํผํฐ์ ๋ฉ์๋๋ ์์์ด๋๋ค
class Human {
static korean = "ํ๊ตญ์ธ";
constructor(name, speed) {
this.name = name;
this.speed = speed;
}
run(speed = 0) {
this.speed += speed;
console.log(`${this.name}์ด ${this.speed}์๋๋ก ๋ฌ๋ฆฐ๋ค`);
}
static compare(humanA, humanB) {
return humanA.speed - humanB.speed;
}
}
class SoccerPlayer extends Human {
goal() {
console.log(`${this.name}์ด ๊ณจ์ ๋ฃ์์ต๋๋ค!`);
}
}
let soccerPlayer = [
new SoccerPlayer("์ํฅ๋ฏผ", 30);
new SoccerPlayer("์ผ์ธ", 25);
];
soccerPlayer.sort(SoccerPlayer.compare);
soccerPlayer[0].run(); // ์ผ์ธ์ด 25์๋๋ก ๋ฌ๋ฆฐ๋ค
console.log(SoccerPlayer.korean); // ํ๊ตญ์ธ
SoccerPlayer.compare๋ฅผ ํธ์ถํ๋ฉด Human.compare๊ฐ ํธ์ถ๋๋ค
์ด๊ฒ ๊ฐ๋ฅํ ์ด์ ๋ extendํค์๋๋ SoccerPlayer์ [[Prototype]]์ด Human์ ์ฐธ์กฐํ๊ฒ ํด์ค๋ค
๋ฐ๋ผ์ SoccerPlay extends Human์ ๋๊ฐ์ [[Prototype]] ์ฐธ์กฐ๋ฅผ ๋ง๋ค์ด๋ธ๋ค
- ํจ์ SoccerPlayer๋ ํฌ๋กํ ํ์ ์ ํตํด ํจ์ Human์ ์์๋ฐ๋๋ค
- SoccerPlayer.prototype์ ํ๋กํ ํ์ ์ ํตํด Human.prototype์ ์์๋ฐ๋๋ค
์ด๋ฐ ๊ณผ์ ์ด ์๊ธฐ ๋๋ฌธ์ ์ผ๋ฐ ๋ฉ์๋ ์์๊ณผ ์ ์ ๋ฉ์๋ ์์์ด ๊ฐ๋ฅํ๋ค
class Human {}
class SoccerPlayer extends Human {}
// ์ ์ ๋ฉ์๋
console.log(SoccerPlayer.__proto__ === Human); // true
// ์ผ๋ฐ ๋ฉ์๋
console.log(SoccerPlayer.__proto__ === Human.prototype); // true
์ ๋ฆฌ
์ ์ ๋ฉ์๋๋ ํน์ ํด๋์ค ์ธ์คํด์ค๊ฐ ์๋ ํด๋์ค ์ ์ฒด์ ํ์ํ ๊ธฐ๋ฅ์ ๋ง๋ค ๋ ์ฌ์ฉํ ์ ์๋ค
์ธ์คํด์ค๋ผ๋ฆฌ ๋น๊ตํด์ฃผ๋ ๋ฉ์๋ Posting.compareDate(postingA, postingB)์ด๋ ํฉํ ๋ฆฌ ๋ฉ์๋ Posting.createToday()๋ฅผ ๋ง๋ค ๋ ์ ์ ๋ฉ์๋๊ฐ ์ฐ์ธ๋ค
์ ์ ๋ฉ์๋๋ ํด๋์ค ์ ์ธ๋ถ ์์ ์์ฐจํ๊ณ ์์ static์ด๋ผ๋ ํค์๋๊ฐ ๋ถ๋๋ค
์ ์ ํ๋กํผํฐ๋ ๋ฐ์ดํฐ๋ฅผ ํด๋์ค ์์ค์ ์ ์ฅํ๊ณ ์ถ์ ๋ ์ฌ์ฉํ๋ค
์ ์ ํ๋กํผํฐ ์ญ์ ๊ฐ๋ณ ์ธ์คํด์ ๋ฌถ์ด์ง ์๋๋ค
class Class {
static property = "property";
static method() {
}
}
// ์์ ๊ฐ๋ค
Class.property = "property"'
Class.method = function() { }
์ ์ ํ๋กํผํฐ์ ์ ์ ๋ฉ์๋๋ ์์์ด ๊ฐ๋ฅํ๋ค
class B extends A๋ ํด๋์ค B์ ํ๋กํ ํ์
์ด ํด๋์ค A๋ฅผ ๊ฐ๋ฆฌํค๊ฒ ํ๋ค (B.[[Prototype]] = A)
๋ฐ๋ผ์ B๊ฐ ์ํ๋ ํ๋กํผํฐ๋ ๋ฉ์๋๋ฅผ ์ฐพ์ง ๋ชปํ๋ฉด A๋ก ๊ฒ์์ด ์ด์ด์ง๋ค
Sources
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/static
static - JavaScript | MDN
The static keyword defines a static method or property for a class, or a class static initialization block (see the link for more information about this usage). Neither static methods nor static properties can be called on instances of the class. Instead,
developer.mozilla.org
https://ko.javascript.info/static-properties-methods
์ ์ ๋ฉ์๋์ ์ ์ ํ๋กํผํฐ
ko.javascript.info
์ ์ ๋ฉ์๋์ ์ ์ ํ๋กํผํฐ
prototype์ด ์๋ ํด๋์ค ํจ์ ์์ฒด์ ๋ฉ์๋๋ฅผ ์ค์ ํ ์ ์๋ค์ด๋ฐ ๋ฉ์๋๋ฅผ ์ ์ (static) ๋ฉ์๋๋ผ๊ณ ๋ถ๋ฅธ๋ค
velog.io