íŽëì€ ìì곌 ìì±ì íšì ìì
ììì ìí íŽëì€ íì¥ì íë¡í íì êž°ë° ìì곌ë ë€ë¥ž ê°ë ìŽë€. íë¡í íì êž°ë° ììì íë¡í íì 첎ìžì íµíŽ ë€ë¥ž ê°ì²Žì ìì°ì ììë°ë ê°ë ìŽì§ë§ ììì ìí íŽëì€ íì¥ì Ʞ졎 íŽëì€ë¥Œ ììë°ì ìë¡ìŽ íŽëì€ë¥Œ íì¥íì¬ ì ìíë ê²ìŽë€.
íŽëì€ì ìì±ì íšìë ìžì€íŽì€ë¥Œ ìì±í ì ìë íšìëŒë ì ìì ì ì¬íì§ë§, íŽëì€ë ììì íµíŽ êž°ì¡Ž íŽëì€ë¥Œ íì¥í ì ìë 묞ë²ìŽ ì ê³µëë ë°ë©Ž, ìì±ì íšìë ê·žë ì§ ìë€.
ì륌 ë€ìŽ, ë묌ì ì¶ìíí Animal
íŽëì€ì ìì ì¬ì륌 ì¶ìíí Bird
, Lion
íŽëì€ë¥Œ ê°ê° ì ìíë€ê³ íŽë³Žì. ìì ì¬ìë ë묌ì ìíë¯ë¡ ë묌ì ìì±ì ê°ë, ê°ì ìì ë§ì ê³ ì í ìì±ë ê°ëë€. ìŽë Animal
íŽëì€ë ë묌ì ìì±ì íííê³ Bird
, Lion
íŽëì€ë ììì íµíŽ Animal
íŽëì€ì ìì±ì ê·žëë¡ ì¬ì©í멎ì ìì ë§ì ê³ ì í ìì±ë§ ì¶ê°íì¬ íì¥í ì ìë€.
ìì ê°ìŽ Bird
íŽëì€ì Lion
íŽëì€ë ììì íµíŽ Animal
íŽëì€ì ìì±ì ê·žëë¡ ì¬ì©íê³ ìì ë§ì ê³ ì í ìì±ë§ ì¶ê°íì¬ íì¥í ì ìë€ë©Ž ìœë ì¬ì¬ì© êŽì ìì ë§€ì° ì ì©í ê²ìŽë€. ì€ì ë¡ ììì íµíŽ Animal
íŽëì€ë¥Œ íì¥í Bird
íŽëì€ë¥Œ 구ííŽ ë³Žì.
class Animal {
constructor(age, weight) {
this.age = age;
this.weight = weight;
}
eat() { return 'eat'; }
move() { return 'move'; }
}
// ììì íµíŽ Animal íŽëì€ë¥Œ íì¥í Bird íŽëì€
class Bird extends Animal {
fly() { return 'fly'; }
}
const bird = new Bird(1, 5);
console.log(bird); // Bird {age: 1, weight: 5}
console.log(bird instanceof Bird); // true
console.log(bird instanceof Animal); // true
console.log(bird.eat()); // eat
console.log(bird.move()); // move
console.log(bird.fly()); // fly
ìŽì²ëŒ íŽëì€ë ììì íµíŽ ë€ë¥ž íŽëì€ë¥Œ íì¥í ì ìë 묞ë²ìž extends
í€ìëê° êž°ë³žì ìŒë¡ ì ê³µëë€. extends
í€ìë륌ì¬ì©í íŽëì€ íì¥ì ê°ížíê³ ì§êŽì ìŽë€. íì§ë§ ìì±ì íšìë íŽëì€ì ê°ìŽ ììì íµíŽ ë€ë¥ž ìì±ì íšì륌 íì¥í ì ìë 묞ë²ìŽ ì ê³µëì§ ìëë€.
ì°žê³ ) ìì¬ íŽëì€ ìì íšíŽ
ìë°ì€í¬ëŠœížë íŽëì€ êž°ë° ìžìŽê° ìëë¯ë¡ ìì±ì íšì륌 ì¬ì©íì¬ íŽëì€ë¥Œ íëŽëŽë €ë ìë륌 ê¶ì¥íì§ ìì§ë§ ìì¬ íŽëì€ ìì íšíŽì ì¬ì©íì¬ ììì ìí íŽëì€ íì¥ì íëŽëŽêž°ë íë€. íŽëì€ì ë±ì¥ìŒë¡ ë€ì ìì ì ê°ì ìì¬ íŽëì€ ìì íšíŽì ëë íìíì§ ìë€.
var Animal = (function () { function Animal(age, weight) { this.age = age; this.weight = weight; } Animal.prototype.eat = function () { return 'eat'; }; Animal.prototype.move = function () { return 'move'; }; return Animal; }()); // Animal ìì±ì íšì륌 ììíì¬ íì¥í Bird ìì±ì íšì var Bird = (function () { function Bird() { // Animal ìì±ì íšììê² thisì ìžì륌 ì ë¬í멎ì ížì¶ Animal.apply(this, arguments); } Bird.prototype = Object.create(Animal.prototype); Bird.prototype.constructor = Bird; Bird.prototype.fly = function () { return 'fly'; }; return Bird; }()); var bird = new Bird(1, 5); console.log(bird); // Bird {age: 1, weight: 5} console.log(bird.eat()); // eat console.log(bird.move());// move console.log(bird.fly()); // flyâ
extends í€ìë
ììì íµíŽ íŽëì€ë¥Œ íì¥íë €ë©Ž extends
í€ìë륌 ì¬ì©íì¬ ììë°ì íŽëì€ë¥Œ ì ìíë€.
// ìíŒ(ë² ìŽì€/ë¶ëªš) íŽëì€
class Base {}
// ìëž(íì/ìì) íŽëì€
class Derived extends Base {}
ììì íµíŽ íì¥ë íŽëì€ë¥Œ ìëžíŽëì€(íì íŽëì€, ìì íŽëì€)ëŒ ë¶ë¥Žê³ , ìëžíŽëì€ìê² ììë íŽëì€ë¥Œ ìíŒíŽëì€(ë² ìŽì€ íŽëì€, ë¶ëªš íŽëì€)ëŒ ë¶ë¥žë€.
extends
í€ìëì ìí ì ìíŒíŽëì€ì ìëžíŽëì€ ê°ì ìì êŽê³ë¥Œ ì€ì íë ê²ìŒë¡, íŽëì€ë íë¡í íì
ì íµíŽ ìì êŽê³ë¥Œ 구ííë€.
ìíŒíŽëì€ì ìëžíŽëì€ë ìžì€íŽì€ì íë¡í íì ì²Žìž ë¿ë§ ìëëŒ íŽëì€ ê°ì íë¡í íì 첎ìžë ìì±íë€. ìŽë¥Œ íµíŽ íë¡í íì ë©ìë, ì ì ë©ìë 몚ë ìììŽ ê°ë¥íë€.
ëì ìì
extends
í€ìëë íŽëì€ë¿ë§ ìëëŒ ìì±ì íšì륌 ììë°ì íŽëì€ë¥Œ íì¥í ìë ìë€. ëš, extends í€ìë ììë ë°ëì íŽëì€ê° ììŒ íë€.
function Base(a) {
this.a = a;
}
// ìì±ì íšì륌 ììë°ë ìëžíŽëì€
class Derived extends Base {}
const derived = new Derived(1);
console.log(derived); // Derived {a: 1}
extends
í€ìë ë€ììë íŽëì€ë¿ë§ ìëëŒ [[Construct]]
ëŽë¶ ë©ìë륌 ê°ë íšì ê°ì²Žë¡ íê°ë ì ìë 몚ë ííìì ì¬ì©í ì ìë€. ìŽë¥Œ íµíŽ ëì ìŒë¡ ììë°ì ëìì ê²°ì í ì ìë€.
function Base1() {}
class Base2 {}
let condition = true;
class Derived extends (condition ? Base1 : Base2) {}
const derived = new Derived();
console.log(derived); // Derived {}
console.log(derived instanceof Base1); // true
console.log(derived instanceof Base2); // false
ìëžíŽëì€ì constructor
íŽëì€ìì constructor
륌 ìëµí멎 íŽëì€ì ë¹ìŽìë constructor
ê° ì묵ì ìŒë¡ ì ìëë€ê³ íë€. ë§ìœ ìëžíŽëì€ìì constructor
륌 ìëµí멎 íŽëì€ì ë€ì곌 ê°ì constructor
ê° ì묵ì ìŒë¡ ì ìëë€. args
ë new
ì°ì°ìì íšê» íŽëì€ë¥Œ ížì¶í ë ì ë¬í ìžìì 늬ì€ížë€.
constructor(...args) { super(...args); }
super()
ë ìíŒíŽëì€ì constructor
(super-constructor)륌 ížì¶íì¬ ìžì€íŽì€ë¥Œ ìì±íë€.
ìë ìì ì ê°ìŽ ìíŒíŽëì€ì ìëžíŽëì€ìì 몚ë constructor
륌 ìëµí멎 ìŽë»ê² ë ê¹?
class Base {}
class Derived extends Base {}
ì ìì ì íŽëì€ìë ì묵ì ìŒë¡ ë€ì곌 ê°ìŽ constructor
ê° ì ìëë€.
class Base {
constructor() {}
}
class Derived extends Base {
constructor(...args) { super(...args); }
}
const derived = new Derived();
console.log(derived); // Derived {}
ìŽì²ëŒ ìíŒíŽëì€ì ìëžíŽëì€ ëªšë constructor
륌 ìëµí멎 ë¹ ê°ì²Žê° ìì±ëë€. ë°ëŒì íë¡íŒí°ë¥Œ ìì íë ìžì€íŽì€ë¥Œ ìì±íë €ë©Ž constructor
ëŽë¶ìì ìžì€íŽì€ì íë¡íŒí°ë¥Œ ì¶ê°íŽìŒ íë€.
super í€ìë
super
í€ìëë íšìì²ëŒ ížì¶í ìë ìê³ this
ì ê°ìŽ ìë³ìì²ëŒ ì°žì¡°í ì ìë í¹ìí í€ìëë€. super
ë ë€ì곌 ê°ìŽ ëìíë€.
super
륌 ížì¶í멎 ìíŒíŽëì€ìconstructor
(super-constructor)륌 ížì¶íë€.super
륌 ì°žì¡°í멎 ìíŒíŽëì€ì ë©ìë륌 ížì¶í ì ìë€.
super ížì¶
super
륌 ížì¶í멎 ìíŒíŽëì€ì constructor
륌 ížì¶íë€.
ìë ìì ì ê°ìŽ ìíŒíŽëì€ì constructor
ëŽë¶ìì ì¶ê°í íë¡íŒí°ë¥Œ ê·žëë¡ ê°ë ìžì€íŽì€ë¥Œ ìì±íë€ë©Ž ìëžíŽëì€ì constructor
륌 ìëµí ì ìë€. ìŽë new
ì°ì°ìì íšê» ìëžíŽëì€ë¥Œ ížì¶í멎ì ì ë¬í ìžìë 몚ë ìëžíŽëì€ì ì묵ì ìŒë¡ ì ìë constructor
ì super
ížì¶ì íµíŽ ìíŒíŽëì€ì constructor
ì ì ë¬ëë€.
class Base {
constructor(a, b) {
this.a = a;
this.b = b;
}
}
class Derived extends Base {
// ë€ì곌 ê°ìŽ ì묵ì ìŒë¡ constructorê° ì ìëš
// constructor(...args) { super(...args); }
}
const derived = new Derived(1, 2);
console.log(derived); // Derived {a: 1, b: 2}
ê·žë¬ë ë€ì ìì ì ê°ìŽ ìíŒíŽëì€ìì ì¶ê°í íë¡íŒí°ì ìëžíŽëì€ìì ì¶ê°í íë¡íŒí°ë¥Œ ê°ë ìžì€íŽì€ë¥Œ ìì±íë€ë©Ž ìëžíŽëì€ì constructor
륌 ìëµí ì ìë€. ìŽë new
ì°ì°ìì íšê» ìëžíŽëì€ë¥Œ ížì¶í멎ì ì ë¬í ìžì ì€ìì ìíŒíŽëì€ì constructor
ì ì ë¬í íìê° ìë ìžìë ìëžíŽëì€ì constructor
ìì ížì¶íë super
륌 íµíŽ ì ë¬íë€.
class Base {
constructor(a, b) { // â£
this.a = a;
this.b = b;
}
}
class Derived extends Base {
constructor(a, b, c) { // â¡
super(a, b); // â¢
this.c = c;
}
}
const derived = new Derived(1, 2, 3); // â
console.log(derived); // Derived {a: 1, b: 2, c: 3}
new
ì°ì°ìì íšê» Derived
íŽëì€ë¥Œ ížì¶(â )í멎ì ì ë¬í ìžì 1, 2, 3ì Derived
íŽëì€ì constructor
(â¡)ì ì ë¬ëê³ super
ížì¶(â¢)ì íµíŽ Base
íŽëì€ì constructor
(â£)ì ìŒë¶ ì ë¬ëë€.
ìŽì²ëŒ ìžì€íŽì€ ìŽêž°í륌 ìíŽ ì ë¬í ìžìë ìíŒíŽëì€ì ìëžíŽëì€ì ë°°ë¶ëê³ ìì êŽê³ì ë íŽëì€ë ìë¡ íë ¥íì¬ ìžì€íŽì€ë¥Œ ìì±íë€.
super륌 ížì¶í ë 죌ìì¬í
- ìëžíŽëì€ìì
constructor
륌 ìëµíì§ ìë ê²œì° ìëžíŽëì€ìconstructor
ììë ë°ëìsuper
륌 ížì¶íŽìŒ íë€. - ìëžíŽëì€ì
constructor
ììsuper
륌 ížì¶íêž° ì ìëthis
륌 ì°žì¡°í ì ìë€. super
ë ë°ëì ìëžíŽëì€ìconstructor
ììë§ ížì¶íë€. ìëžíŽëì€ê° ìë íŽëì€ìconstructor
ë íšìììsuper
륌 ížì¶í멎 ìë¬ê° ë°ìíë€.
super ì°žì¡°
ë©ìë ëŽìì super
륌 ì°žì¡°í멎 ìíŒíŽëì€ì ë©ìë륌 ížì¶í ì ìë€.
íë¡í íì ë©ìë ëŽìì super ì°žì¡°
ìëžíŽëì€ì íë¡í íì
ë©ìë ëŽìì super.sayHi
ë ìíŒíŽëì€ì íë¡í íì
ë©ìë sayHi
륌 ê°ëЬíšë€.
class Base {
constructor(name) {
this.name = name;
}
sayHi() {
return 'Hi! ' + this.name;
}
}
class Derived extends Base {
sayHi() {
return super.sayHi() + '. how are you doing?';
}
}
const derived = new Derived('Jung');
console.log(derived.sayHi()); // Hi! Jung. how are you doing?
super
찞조륌 íµíŽ ìíŒíŽëì€ì ë©ìë륌 ì°žì¡°íë €ë©Ž super
ê° ìíŒíŽëì€ì ë©ìëê° ë°ìžë©ë ê°ì²Ž, ìŠ ìíŒíŽëì€ì prototype
íë¡íŒí°ì ë°ìžë©ë íë¡í íì
ì ì°žì¡°í ì ììŽìŒ íë€. ì ìì ë ë€ì ìì ì ëìŒíê² ëìíë€.
class Base {
constructor(name) {
this.name = name;
}
sayHi() {
return 'Hi! ' + this.name;
}
}
class Derived extends Base {
sayHi() {
const __super = Object.getPrototypeOf(Derived.prototype);
return __super.sayHi.call(this) + ' how are you doing?';
}
}
super
ë ìì ì ì°žì¡°íê³ ìë ë©ìë(ì ìì ì ê²œì° Derived
ì sayHi
)ê° ë°ìžë©ëìŽ ìë ê°ì²Ž(ì ìì ì ê²œì° Derived.prototype
)ì íë¡í íì
(ì ìì ì ê²œì° Base.prototype
)ì ê°ëЬíšë€. ë°ëŒì super.sayHi
ë Base.prototype.sayHi
륌 ê°ëЬíšë€. ëš, super.sayHi
, ìŠ Base.prototype.sayHi
륌 ížì¶í ë call
ë©ìë륌 ì¬ì©íŽ this
륌 ì ë¬íŽìŒ íë€.
call
ë©ìë륌 ì¬ì©íŽ this
륌 ì ë¬íì§ ìê³ Base.prototype.sayHi
륌 ê·žëë¡ ížì¶í멎 Base.prototype.sayHi
ë©ìë ëŽë¶ì this
ë Base.prototype
ì ê°ëЬíšë€. Base.prototype.sayHi
ë©ìëë íë¡í íì
ë©ìëìŽêž° ë묞ì ëŽë¶ì this
ë Base.prototype
ìŽ ìë ìžì€íŽì€ë¥Œ ê°ëЬìŒìŒ íë€. name
íë¡íŒí°ë ìžì€íŽì€ì 졎ì¬íêž° ë묞ìŽë€.
ìŽì²ëŒ super
ì°žì¡°ê° ëìíêž° ìíŽìë super
륌 ì°žì¡°íê³ ìë ë©ìë(ì ìì ì ê²œì° Derived
ì sayHi
)ê° ë°ìžë©ëìŽ ìë ê°ì²Ž(ì ìì ì ê²œì° Derived.prototype
)ì íë¡í íì
(ì ìì ì ê²œì° Base.prototype
)ì ì°Ÿì ì ììŽìŒ íë€. ìŽë¥Œ ìíŽ ë©ìëë ëŽë¶ ì¬ë¡¯ [[HomeObject]]
륌 ê°ì§ë©°, ìì ì ë°ìžë©íê³ ìë ê°ì²Žë¥Œ ê°ëЬíšë€.
ìŽë¥Œ ë°íìŒë¡ super
찞조륌 ìì¬ ìœëë¡ ííí멎 ë€ì곌 ê°ë€.
/*
[[HomeObject]]ë ë©ìë ìì ì ë°ìžë©íê³ ìë ê°ì²Žë¥Œ ê°ëЬíšë€.
[[HomeObject]]륌 íµíŽ ë©ìë ìì ì ë°ìžë©íê³ ìë ê°ì²Žì íë¡í íì
ì ì°¿ì ì ìë€.
ì륌 ë€ìŽ, Derived íŽëì€ì sayHi ë©ìëë Derived.prototypeì ë°ìžë©ëìŽ ìë€.
ë°ëŒì Derived íŽëì€ì sayHi ë©ìëì [[HomeObject]]ë Derived.prototypeìŽê³ ,
ìŽë¥Œ íµíŽ Derived íŽëì€ì sayHi ë©ìë ëŽë¶ì super ì°žì¡°ê° Base.prototypeìŒë¡ ê²°ì ëë€.
ë°ëŒì super.sayHië Base.prototype.sayHi륌 ê°ëЬí€ê² ëë€.
*/
super = Object.getPrototypeOf([[HomeObject]])
죌ìí ì ì ES6ì ë©ìë ì¶ìœ ííìŒë¡ ì ìë íšìë§ìŽ [[HomeObject]]
륌 ê°ëë€ë ê²ìŽë€.
const obj = {
// fooë ES6 ë©ìë ì¶ìœ ííìŒë¡ ì ìí ë©ìëìŽë¯ë¡ [[HomeObject]]륌 ê°ì§
foo() {},
// barë ES6ì ë©ìë ì¶ìœ ííìŒë¡ ì ìí ë©ìëê° ìë ìŒë°íšì
// [[HomeObject]]륌 ê°ì§ ìëë€.
bar: function () {}
};
[[HomeObject]]
륌 ê°ì§ë íšìë§ìŽ super
찞조륌 í ì ììŒë¯ë¡, ES6ì ë©ìë ì¶ìœ ííìŒë¡ ì ìë íšìë§ìŽ super
찞조륌 í ì ìë€. ë€ë§ super
ì°žì¡°ë ìíŒíŽëì€ì ë©ìë륌 ì°žì¡°íêž° ìíŽ ì¬ì©íë¯ë¡ ìëžíŽëì€ì ë©ìëìì ì¬ì©íŽìŒ íë€.
super
ì°žì¡°ë íŽëì€ì ì ì ë¬ŒìŽ ìëë©°, ê°ì²Ž 늬í°ëŽììë super
찞조륌 ì¬ì©í ì ìë€. ìŽëë ë§ì°¬ê°ì§ë¡ ES6 ë©ìë ì¶ìœ ííìŒë¡ ì ìë íšìë§ ê°ë¥íë€.
const base = {
name: 'Jung',
sayHi() {
return 'Hi! ' + this.name;
}
};
const derived = {
__proto__: base,
sayHi() {
return super.sayHi() + '. how are you doing?';
}
};
console.log(derived.sayHi()); // Hi! Jung. how are you doing?
ì ì ë©ìë ëŽìì super ì°žì¡°
ìëžíŽëì€ì ì ì ë©ìë ëŽìì super.sayHi
ë ìíŒíŽëì€ì ì ì ë©ìë sayHi
륌 ê°ëЬíšë€.
class Base {
static sayHi() {
return 'Hi!';
}
}
class Derived extends Base {
static sayHi() {
return super.sayHi() + ' how are you doing?';
}
}
console.log(Derived.sayHi()); // Hi! how are you doing?
ìì íŽëì€ì ìžì€íŽì€ ìì± ê³Œì
ìì êŽê³ì ìë ë íŽëì€ë ìŽë»ê² íë ¥íë©° ìžì€íŽì€ë¥Œ ìì±í ê¹?
ìŽì ì ìŽíŽë³ž íŽëì€ì ìžì€íŽì€ ìì± ê³Œì ììë íŽëì€ê° ëšë ìŒë¡ ìžì€íŽì€ë¥Œ ìì±íë 곌ì ì ìì뎀ë€. íì§ë§ ìì êŽê³ì ìë ë íŽëì€ê° íë ¥íë©° ìžì€íŽì€ë¥Œ ìì±íë 곌ì ì ì¢ ë ë³µì¡íë€.
ìëë ì§ì¬ê°íì ì¶ìíí Rectangle
íŽëì€ì ììì íµíŽ Rectangle
íŽëì€ë¥Œ íì¥í ColorRectangle
íŽëì€ë¥Œ ì ìí ìì ë€.
// ìíŒíŽëì€
class Rectangle {
constructor(width, height) {
this.width = width;
this.height = height;
}
getArea() {
return this.width * this.height;
}
toString() {
return 'width = ' + this.width + ', height = ' + this.height;
}
}
// ìëžíŽëì€
class ColorRectangle extends Rectangle {
constructor(width, height, color) {
super(width, height);
this.color = color;
}
// ë©ìë ì€ë²ëŒìŽë©
toString() {
return super.toString() + ', color = ' + this.color
}
}
const colorRectangle = new ColorRectangle(2, 4, 'red');
console.log(colorRectangle); // ColorRectangle {width: 2, height: 4, color: "red"}
// ììì íµíŽ getArea ë©ìë ížì¶
console.log(colorRectangle.getArea()); // 8
console.log(colorRectangle.toString()); // width = 2, height = 4, color = red
ìëž íŽëì€ ColorRectangle
ìŽ new
ì°ì°ìì íšê» ížì¶ë멎 ë€ì 곌ì ì íµíŽ ìžì€íŽì€ë¥Œ ìì±íë€.
1. ìëžíŽëì€ì super ížì¶
ìë°ì€í¬ëŠœíž ìì§ì íŽëì€ë¥Œ íê°í ë ìíŒíŽëì€ì ìëžíŽëì€ë¥Œ 구ë¶íêž° ìíŽ "base"
ëë "derived"
륌 ê°ìŒë¡ ê°ë ëŽë¶ ì¬ë¡¯ [[ConstructorKind]]
륌 ê°ëë€. ë€ë¥ž íŽëì€ë¥Œ ììë°ì§ ìë íŽëì€(ë° ìì±ì íšì)ë ëŽë¶ ì¬ë¡¯ [[ConstructorKind]]
ì ê°ìŽ "base"
ë¡ ì€ì ëë©°, ë€ë¥ž íŽëì€ë¥Œ ììë°ë ìëžíŽëì€ë "derived"
ë¡ ì€ì ëë€. ìŽë¥Œ íµíŽ ìíŒíŽëì€ì ìëžíŽëì€ë new
ì°ì°ìì íšê» ížì¶ëìì ëì ëììŽ êµ¬ë¶ëë€.
ë€ë¥ž íŽëì€ë¥Œ ììë°ì§ ìë íŽëì€(ë° ìì±ì íšì)ë new
ì°ì°ìì íšê» ížì¶ëìì ë ì묵ì ìŒë¡ ë¹ ê°ì²Ž, ìŠ ìžì€íŽì€ë¥Œ ìì±íê³ ìŽë¥Œ this
ì ë°ìžë©íë€.
íì§ë§ ìëžíŽëì€ë ìì ìŽ ì§ì ìžì€íŽì€ë¥Œ ìì±íì§ ìê³ ìíŒíŽëì€ìê² ìžì€íŽì€ ìì±ì ììíë€. ìŽê²ìŽ ë°ë¡ ìëžíŽëì€ì constructor
ìì ë°ëì super
륌 ížì¶íŽìŒ íë ìŽì ë€.
ìëžíŽëì€ê° new
ì°ì°ìì íšê» ížì¶ë멎 ìëžíŽëì€ constructor
ëŽë¶ì super
í€ìëê° íšìì²ëŒ ížì¶ëë€. super
ê° ížì¶ë멎 ìíŒíŽëì€ì constructor
(super-constructor)ê° ížì¶ëë€. ì¢ ë ì íí ë§íì멎 ìíŒíŽëì€ê° íê°ëìŽ ìì±ë íšì ê°ì²Žì ìœëê° ì€íëêž° ììíë€.
ë§ìœ ìëžíŽëì€ constructor
ëŽë¶ì super
ížì¶ìŽ ììŒë©Ž ìë¬ê° ë°ìíë€. ì€ì ë¡ ìžì€íŽì€ë¥Œ ìì±íë 죌첎ë ìíŒíŽëì€ìŽë¯ë¡ ìíŒíŽëì€ì constructor
륌 ížì¶íë super
ê° ížì¶ëì§ ììŒë©Ž ìžì€íŽì€ë¥Œ ìì±í ì ìêž° ë묞ìŽë€.
2. ìíŒíŽëì€ì ìžì€íŽì€ ìì±ê³Œ this ë°ìžë©
ìíŒíŽëì€ì constructor
ëŽë¶ì ìœëê° ì€íëêž° ìŽì ì ì묵ì ìŒë¡ ë¹ ê°ì²Žë¥Œ ìì±íë€. ìŽ ë¹ ê°ì²Žê° ë°ë¡ ìì§ ë¯žìì±ë ìíìž íŽëì€ê° ìì±í ìžì€íŽì€ë€. ê·žëŠ¬ê³ ì묵ì ìŒë¡ ìì±ë ìŽ ë¹ ê°ì²Ž, ìŠ ìžì€íŽì€ë this
ì ë°ìžë©ëë€. ë°ëŒì ìíŒíŽëì€ì constructor
ëŽë¶ì this
ë ìì±ë ìžì€íŽì€ë¥Œ ê°ëЬíšë€.
// ìíŒíŽëì€
class Rectangle {
constructor(width, height) {
// ì묵ì ìŒë¡ ë¹ ê°ì²Ž, ìŠ ìžì€íŽì€ê° ìì±ëê³ thisì ë°ìžë©ëë€.
console.log(this); // ColorRectangle {}
// new ì°ì°ìì íšê» ížì¶ë íšì, ìŠ new.targetì ColorRectangleìŽë€.
console.log(new.target); // ColorRectangle
(...)
ìŽë ìžì€íŽì€ë ìíŒíŽëì€ê° ìì±í ê²ìŽì§ë§, new
ì°ì°ìì íšê» ížì¶ë íŽëì€ë ìëžíŽëì€ëŒë ê²ìŽ ì€ìíë€. ìŠ, new
ì°ì°ìì íšê» ížì¶ë íšì륌 ê°ëЬí€ë new.target
ì ìëžíŽëì€ë¥Œ ê°ëЬíšë€. ë°ëŒì ìžì€íŽì€ë new.target
ìŽ ê°ëЬí€ë ìëžíŽëì€ê° ìì±í ê²ìŒë¡ ì²ëЬëë€.
ë°ëŒì ìì±ë ìžì€íŽì€ì íë¡í íì
ì ìíŒíŽëì€ì prototype
íë¡íŒí°ê° ê°ëЬí€ë ê°ì²Ž(Rectangle.prototype
)ê° ìëëŒ new.target
, ìŠ ìëžíŽëì€ì prototype
íë¡íŒí°ê° ê°ëЬí€ë ê°ì²Ž(ColorRectangle.prototype
)ë€.
// ìíŒíŽëì€
class Rectangle {
constructor(width, height) {
// ì묵ì ìŒë¡ ë¹ ê°ì²Ž, ìŠ ìžì€íŽì€ê° ìì±ëê³ thisì ë°ìžë©ëë€.
console.log(this); // ColorRectangle {}
// new ì°ì°ìì íšê» ížì¶ë íšì, ìŠ new.targetì ColorRectangleìŽë€.
console.log(new.target); // ColorRectangle
// ìì±ë ìžì€íŽì€ì íë¡í íì
ìŒë¡ ColorRectangle.prototypeìŽ ì€ì ëë€.
console.log(Object.getPrototypeOf(this) === ColorRectangle.prototype); // true
console.log(this instanceof ColorRectangle); // true
console.log(this instanceof Rectangle); // true
(...)
3. ìíŒíŽëì€ì ìžì€íŽì€ ìŽêž°í
ìíŒíŽëì€ì constructor
ê° ì€íëìŽ this
ì ë°ìžë©ëìŽ ìë ìžì€íŽì€ë¥Œ ìŽêž°ííë€. ìŠ, this
ì ë°ìžë©ëìŽ ìë ìžì€íŽì€ì íë¡íŒí°ë¥Œ ì¶ê°íê³ constructor
ê° ìžìë¡ ì ë¬ë°ì ìŽêž°ê°ìŒë¡ ìžì€íŽì€ì íë¡íŒí°ë¥Œ ìŽêž°ííë€.
// ìíŒíŽëì€
class Rectangle {
constructor(width, height) {
// ì묵ì ìŒë¡ ë¹ ê°ì²Ž, ìŠ ìžì€íŽì€ê° ìì±ëê³ thisì ë°ìžë©ëë€.
console.log(this); // ColorRectangle {}
// new ì°ì°ìì íšê» ížì¶ë íšì, ìŠ new.targetì ColorRectangleìŽë€.
console.log(new.target); // ColorRectangle
// ìì±ë ìžì€íŽì€ì íë¡í íì
ìŒë¡ ColorRectangle.prototypeìŽ ì€ì ëë€.
console.log(Object.getPrototypeOf(this) === ColorRectangle.prototype); // true
console.log(this instanceof ColorRectangle); // true
console.log(this instanceof Rectangle); // true
// ìžì€íŽì€ ìŽêž°í
this.width = width;
this.height = height;
console.log(this); // ColorRectangle {width: 2, height: 4}
}
(...)
4. ìëžíŽëì€ constructorë¡ì ë³µê·ì this ë°ìžë©
super
ì ížì¶ìŽ ì¢
ë£ëê³ ì ìŽ íëŠìŽ ìëžíŽëì€ constructor
ë¡ ëììšë€. ìŽë super
ê° ë°íí ìžì€íŽì€ê° this
ì ë°ìžë©ëë€. ìëžíŽëì€ë ë³ëì ìžì€íŽì€ë¥Œ ìì±íì§ ìê³ super
ê° ë°íí ìžì€íŽì€ë¥Œ this
ì ë°ìžë©íì¬ ê·žëë¡ ì¬ì©íë€.
class ColorRectangle extends Rectangle {
constructor(width, height, color) {
super(width, height);
// superê° ë°íí ìžì€íŽì€ê° thisì ë°ìžë©ëë€.
console.log(this); // ColorRectangle {width: 2, height: 4}
(...)
ìŽì²ëŒ super
ê° ížì¶ëì§ ììŒë©Ž ìžì€íŽì€ê° ìì±ëì§ ììŒë©°, this
ë°ìžë©ë í ì ìë€. ìëžíŽëì€ì constructor
ìì super
륌 ížì¶íêž° ì ìë this
륌 ì°žì¡°í ì ìë ìŽì ê° ë°ë¡ ìŽ ë묞ìŽë€. ë°ëŒì ìëžíŽëì€ constructor
ëŽë¶ì ìžì€íŽì€ ìŽêž°íë ë°ëì super
ížì¶ ìŽíì ì²ëЬëìŽìŒ íë€.
5. ìëžíŽëì€ì ìžì€íŽì€ ìŽêž°í
super
ížì¶ ìŽí, ìëžíŽëì€ì constructor
ì êž°ì ëìŽ ìë ìžì€íŽì€ ìŽêž°íê° ì€íëë€. ìŠ, this
ì ë°ìžë©ëìŽ ìë ìžì€íŽì€ì íë¡íŒí°ë¥Œ ì¶ê°íê³ constructor
ê° ìžìë¡ ì ë¬ë°ì ìŽêž°ê°ìŒë¡ ìžì€íŽì€ì íë¡íŒí°ë¥Œ ìŽêž°ííë€.
6. ìžì€íŽì€ ë°í
íŽëì€ì 몚ë ì²ëŠ¬ê° ëë멎 ìì±ë ìžì€íŽì€ê° ë°ìžë©ë this
ê° ì묵ì ìŒë¡ ë°íëë€.
// ìëžíŽëì€
class ColorRectangle extends Rectangle {
constructor(width, height, color) {
super(width, height);
// superê° ë°íí ìžì€íŽì€ê° thisì ë°ìžë©ëë€.
console.log(this); // ColorRectangle {width: 2, height: 4}
// ìžì€íŽì€ ìŽêž°í
this.color = color;
// ìì±ë ìžì€íŽì€ê° ë°ìžë©ë thisê° ì묵ì ìŒë¡ ë°íëë€.
console.log(this); // ColorRectangle {width: 2, height: 4, color: "red"}
}
(...)
íì€ ë¹ížìž ìì±ì íšì íì¥
ìì ëì ìììì ìŽíŽë³Žìë¯, extends
í€ìë ë€ììë íŽëì€ë¿ë§ ìëëŒ [[Constructor]]
ëŽë¶ ë©ìë륌 ê°ë íšì ê°ì²Žë¡ íê°ë ì ìë 몚ë ííìì ì¬ì©í ì ìë€. String
, Number
, Array
ê°ì íì€ ë¹ížìž ê°ì²Žë [[Constructor]]
ëŽë¶ ë©ìë륌 ê°ë ìì±ì íšììŽë¯ë¡ extends
í€ìë륌 ì¬ì©íì¬ íì¥í ì ìë€.
// Array ìì±ì íšì륌 ììë°ì íì¥í MyArray
class MyArray extends Array {
// ì€ë³µë ë°°ìŽ ìì륌 ì ê±°íê³ ë°ííë€: [1, 1, 2, 3] => [1, 2, 3]
uniq() {
return this.filter((v, i, self) => self.indexOf(v) === i);
}
// 몚ë ë°°ìŽ ììì íê· ì 구íë€: [1, 2, 3] => 2
average() {
return this.reduce((pre, cur) => pre + cur, 0) / this.length;
}
}
const myArray = new MyArray(1, 1, 2, 3);
console.log(myArray); // MyArray(4) [1, 1, 2, 3]
// MyArray.prototype.uniq ížì¶
console.log(myArray.uniq()); // MyArray(3) [1, 2, 3]
// MyArray.prototype.average ížì¶
console.log(myArray.average()); // 1.75
Array
ìì±ì íšì륌 ììë°ì íì¥í MyArray
íŽëì€ê° ìì±í ìžì€íŽì€ë Array.prototype
곌 MyArray.prototype
ì 몚ë ë©ìë륌 ì¬ì©í ì ìë€.
ìŽë 죌ìí ê²ì Array.prototype
ì ë©ìë ì€ìì map
, filter
ì ê°ìŽ ìë¡ìŽ ë°°ìŽì ë°ííë ë©ìëê° MyArray
íŽëì€ì ìžì€íŽì€ë¥Œ ë°ííë€ë ê²ìŽë€.
console.log(myArray.filter(v => v % 2) instanceof MyArray); // true
ë§ìœ ìë¡ìŽ ë°°ìŽì ë°ííë ë©ìëê° MyArray
íŽëì€ì ìžì€íŽì€ë¥Œ ë°ííì§ ìê³ Array
ì ìžì€íŽì€ë¥Œ ë°íí멎 MyArray
íŽëì€ì ë©ìëì ë©ìë 첎ìŽëìŽ ë¶ê°ë¥íë€.
// ë©ìë 첎ìŽë
// [1, 1, 2, 3] => [1, 1, 3] => [1, 3] => 2
console.log(myArray.filter(v => v % 2).uniq().average()); // 2
myArray.filter
ê° ë°ííë ìžì€íŽì€ë MyArray
íŽëì€ê° ìì±í ìžì€íŽì€, ìŠ MyArray
íì
ìŽë€. ë°ëŒì myArray.filter
ê° ë°ííë ìžì€íŽì€ë¡ uniq
ë©ìë륌 ì°ìŽìŽ ížì¶í ì ìë€. uniq
ë©ìëê° ë°ííë ìžì€íŽì€ë MyArray
íì
ìŽë€. ë°ëŒì uniq
ë©ìëê° ë°ííë ìžì€íŽì€ë¡ average
ë©ìë륌 ì°ìŽìŽ ížì¶í ì ìë€.
ë§ìœ MyArray
íŽëì€ì uniq
ë©ìëê° MyArray
íŽëì€ê° ìì±í ìžì€íŽì€ê° ìë Array
ê° ìì±í ìžì€íŽì€ë¥Œ ë°ííê² íë €ë©Ž ë€ì곌 ê°ìŽ Symbol.species
륌 ì¬ì©íì¬ ì ì ì ê·Œì íë¡íŒí°ë¥Œ ì¶ê°íŽìŒ íë€.
// Array ìì±ì íšì륌 ììë°ì íì¥í MyArray
class MyArray extends Array {
// 몚ë ë©ìëê° Array íì
ì ìžì€íŽì€ë¥Œ ë°ííëë¡ íë€.
static get [Symbol.species]() { return Array; }
uniq() {
return this.filter((v, i, self) => self.indexOf(v) === i);
}
average() {
return this.reduce((pre, cur) => pre + cur, 0) / this.length;
}
}
const myArray = new MyArray(1, 1, 2, 3);
console.log(myArray.uniq() instanceof MyArray); // false
console.log(myArray.uniq() instanceof Array); // true
// ë©ìë 첎ìŽë
// uniq ë©ìëë Array ìžì€íŽì€ë¥Œ ë°ííë¯ë¡ average ë©ìë륌 ížì¶í ì ìë€.
console.log(myArray.uniq().average());
// TypeError: myArray.uniq(...).average is not a function
ì°žê³
- ì ì
- ìŽì 몚
- ì¶í
- ìí€ë¶ì€
- ì¶íìŒ
- 2020.09.25