"prototype"ì´ ìë í´ëì¤ í¨ì ìì²´ì ë©ìë를 ì¤ì í ìë ììµëë¤. ì´ë° ë©ìë를 ì ì (static) ë©ìëë¼ê³ ë¶ë¦
ëë¤.
ì ì ë©ìëë ìëì ê°ì´ í´ëì¤ ììì static í¤ìë를 ë¶ì¬ ë§ë¤ ì ììµëë¤.
class User {
static staticMethod() {
alert(this === User);
}
}
User.staticMethod(); // true
ì ì ë©ìëë ë©ìë를 íë¡í¼í° ííë¡ ì§ì í ë¹íë ê²ê³¼ ëì¼í ì¼ì í©ëë¤.
class User { }
User.staticMethod = function() {
alert(this === User);
};
User.staticMethod(); // true
User.staticMethod()ê° í¸ì¶ë ë thisì ê°ì í´ëì¤ ìì±ìì¸ User ìì²´ê° ë©ëë¤(ì ì ê°ì²´).
ì ì ë©ìëë ì´ë¤ í¹ì í ê°ì²´ê° ìë í´ëì¤ì ìí í¨ì를 구ííê³ ì í ë ì£¼ë¡ ì¬ì©ë©ëë¤.
ê°ì²´ Articleì´ ì¬ë¬ ê° ìê³ ì´ë¤ì ë¹êµí´ì¤ í¨ìê° íìíë¤ê³ ê°ì í´ ë´
ìë¤. ê°ì¥ 먼ì ìëì ê°ì´ Article.compare를 ì¶ê°íë ë°©ë²ì´ ë ì¤ë¥¼ ê²ëë¤.
class Article {
constructor(title, date) {
this.title = title;
this.date = date;
}
static compare(articleA, articleB) {
return articleA.date - articleB.date;
}
}
// ì¬ì©ë²
let articles = [
new Article("HTML", new Date(2019, 1, 1)),
new Article("CSS", new Date(2019, 0, 1)),
new Article("JavaScript", new Date(2019, 11, 1))
];
articles.sort(Article.compare);
alert( articles[0].title ); // CSS
ì¬ê¸°ì Article.compareë article(ê¸)ì ë¹êµí´ì£¼ë ìë¨ì¼ë¡, ê¸ ì 체를 âìììâ ë°ë¼ë³´ë©° ë¹êµë¥¼ ìíí©ëë¤. Article.compareì´ ê¸ íëì ë©ìëê° ìë í´ëì¤ì ë©ìëì¬ì¼ íë ì´ì ê° ì¬ê¸°ì ììµëë¤.
ì´ë²ì ì´í´ë³¼ ììë âí©í 리â ë©ìë를 구íí ì½ëì ëë¤. ë¤ìí ë°©ë²ì ì¬ì©í´ ì¡°ê±´ì ë§ë article ì¸ì¤í´ì¤ë¥¼ ë§ë¤ì´ì¼ íë¤ê³ ê°ì í´ ë´ ìë¤.
- 매ê°ë³ì(
title,dateë±)를 ì´ì©í´ ê´ë ¨ ì ë³´ê° ë´ê¸´ article ìì± - ì¤ë ë ì§ë¥¼ 기ë°ì¼ë¡ ë¹ì´ìë article ìì±
- 기í ë±ë±
첫 ë²ì§¸ ë°©ë²ì ìì±ì를 ì¬ì©í´ 구íí ì ììµëë¤. ë ë²ì§¸ ë°©ë²ì í´ëì¤ì ì ì ë©ìë를 ë§ë¤ì´ 구íí ì ììµëë¤.
ìë Article.createTodays()ê°ì´ ë§ì´ì£ .
class Article {
constructor(title, date) {
this.title = title;
this.date = date;
}
static createTodays() {
// thisë Articleì
ëë¤.
return new this("Today's digest", new Date());
}
}
let article = Article.createTodays();
alert( article.title ); // Today's digest
ì´ì Todayâs digestë¼ë ê¸ì´ íìí ëë§ë¤ Article.createTodays()를 í¸ì¶íë©´ ë©ëë¤. ì¬ê¸°ìë ë§ì°¬ê°ì§ë¡ Article.createTodays()ë articleì ë©ìëê° ìë ì ì²´ í´ëì¤ì ë©ìëì
ëë¤.
ì ì ë©ìëë ìë ììì ê°ì´ í목 ê²ì, ì ì¥, ìì ë±ì ìíí´ì£¼ë ë°ì´í°ë² ì´ì¤ ê´ë ¨ í´ëì¤ìë ì¬ì©ë©ëë¤.
// Articleì articleì ê´ë¦¬í´ì£¼ë í¹ë³ í´ëì¤ë¼ê³ ê°ì í©ìë¤.
// article ìì ì ì°ì´ë ì ì ë©ìë
Article.remove({id: 12345});
ì ì íë¡í¼í°
ì ì íë¡í¼í°ë ë¬¼ë¡ ë§ë¤ ì ììµëë¤. ì ì íë¡í¼í°ë ì¼ë° í´ëì¤ íë¡í¼í°ì ì ì¬íê² ìê²¼ëë° ìì staticì´ ë¶ëë¤ë ì ë§ ë¤ë¦
ëë¤.
class Article {
static publisher = "Ilya Kantor";
}
alert( Article.publisher ); // Ilya Kantor
ì ììë Articleì íë¡í¼í°ë¥¼ ì§ì í ë¹í ê²ê³¼ ëì¼íê² ëìí©ëë¤.
Article.publisher = "Ilya Kantor";
ì ì íë¡í¼í°ì ë©ìë ìì
ì ì íë¡í¼í°ì ë©ìëë ììë©ëë¤.
ìë ìììì Animal.compareì Animal.planetì ììëì´ì ê°ê° Rabbit.compareì Rabbit.planetìì ì ê·¼í ì ììµëë¤.
class Animal {
static planet = "ì§êµ¬";
constructor(name, speed) {
this.speed = speed;
this.name = name;
}
run(speed = 0) {
this.speed += speed;
alert(`${this.name}ê° ìë ${this.speed}ë¡ ë¬ë¦½ëë¤.`);
}
static compare(animalA, animalB) {
return animalA.speed - animalB.speed;
}
}
// Animalì ììë°ì
class Rabbit extends Animal {
hide() {
alert(`${this.name}ê° ì¨ììµëë¤!`);
}
}
let rabbits = [
new Rabbit("í° í ë¼", 10),
new Rabbit("ê²ì í ë¼", 5)
];
rabbits.sort(Rabbit.compare);
rabbits[0].run(); // ê²ì í ë¼ê° ìë 5ë¡ ë¬ë¦½ëë¤.
alert(Rabbit.planet); // ì§êµ¬
ì´ì Rabbit.compareì í¸ì¶íë©´ Animal.compareê° í¸ì¶ë©ëë¤.
ì´ê² ê°ë¥í ì´ì ë íë¡í íì
ë문ì
ëë¤. ì´ë¯¸ ììíì
¨ê² ì§ë§, extends í¤ìëë Rabbitì [[Prototype]]ì´ Animalì 참조íëë¡ í´ì¤ëë¤.
ë°ë¼ì Rabbit extends Animalì ë ê°ì [[Prototype]] 참조를 ë§ë¤ì´ ë
ëë¤.
- í¨ì
Rabbitì íë¡í íì ì íµí´ í¨ìAnimalì ììë°ìµëë¤. Rabbit.prototypeì íë¡í íì ì íµí´Animal.prototypeì ììë°ìµëë¤.
ì´ë° ê³¼ì ì´ ì기 ë문ì ì¼ë° ë©ìë ììê³¼ ì ì ë©ìë ììì´ ê°ë¥í©ëë¤.
ì½ëë¡ ì§ì íì¸í´ë´ ìë¤.
class Animal {}
class Rabbit extends Animal {}
// ì ì ë©ìë
alert(Rabbit.__proto__ === Animal); // true
// ì¼ë° ë©ìë
alert(Rabbit.prototype.__proto__ === Animal.prototype); // true
ìì½
ì ì ë©ìëë í¹ì í´ëì¤ ì¸ì¤í´ì¤ê° ìë í´ëì¤ 'ì ì²´âì íìí 기ë¥ì ë§ë¤ ë ì¬ì©í ì ììµëë¤.
ì¸ì¤í´ì¤ë¼ë¦¬ ë¹êµí´ì£¼ë ë©ìë Article.compare(article1, article2)ì´ë í©í 리 ë©ìë Article.createTodays()를 ë§ë¤ ë ì ì ë©ìëê° ì°ì
ëë¤.
ì ì ë©ìëë í´ëì¤ ì ì¸ë¶ ìì ìì¹íê³ ìì staticì´ë¼ë í¤ìëê° ë¶ìµëë¤.
ì ì íë¡í¼í°ë ë°ì´í°ë¥¼ í´ëì¤ ìì¤ì ì ì¥íê³ ì¶ì ë ì¬ì©í©ëë¤. ì ì íë¡í¼í° ìì ê°ë³ ì¸ì¤í´ì¤ì 묶ì´ì§ ììµëë¤.
문ë²:
class MyClass {
static property = ...;
static method() {
...
}
}
staticì ì¬ì©í ì ì¸ì 기ì ì ì¼ë¡ í´ëì¤ ìì²´ì ì§ì í ë¹íë ê²ê³¼ ëì¼í©ëë¤.
MyClass.property = ...
MyClass.method = ...
ì ì íë¡í¼í°ì ì ì ë©ìëë ììì´ ê°ë¥í©ëë¤.
class B extends Aë í´ëì¤ Bì íë¡í íì
ì´ í´ëì¤ A를 ê°ë¦¬í¤ê² í©ëë¤(B.[[Prototype]] = A). ë°ë¼ì Bìì ìíë íë¡í¼í°ë ë©ìë를 ì°¾ì§ ëª»íë©´ Aë¡ ê²ìì´ ì´ì´ì§ëë¤.
ëê¸
<code>í그를, ì¬ë¬ ì¤ë¡ 구ì±ë ì½ë를 ì½ì íê³ ì¶ë¤ë©´<pre>í그를 ì´ì©íì¸ì. 10ì¤ ì´ìì ì½ëë plnkr, JSBin, codepen ë±ì ìëë°ì¤ë¥¼ ì¬ì©íì¸ì.