Baseline Widely available
super
í¤ìëë ê°ì²´ 리í°ë´ ëë í´ëì¤ì [[Prototype]] ìì±ì ì ê·¼íê±°ë ìí¼í´ëì¤ì ìì±ì를 í¸ì¶íë ë° ì¬ì©ë©ëë¤.
super.prop
ì super[expr]
ííìì í´ëì¤ì ê°ì²´ 리í°ë´ì ë©ìë ì ììì 모ë ì¬ì©í ì ììµëë¤. super(...args)
ííìì í´ëì¤ ìì±ììì ì í¨í©ëë¤.
class Foo {
constructor(name) {
this.name = name;
}
getNameSeparator() {
return "-";
}
}
class FooBar extends Foo {
constructor(name, index) {
super(name);
this.index = index;
}
// Does not get called
getNameSeparator() {
return "/";
}
getFullName() {
return this.name + super.getNameSeparator() + this.index;
}
}
const firstFooBar = new FooBar("foo", 1);
console.log(firstFooBar.name);
// Expected output: "foo"
console.log(firstFooBar.getFullName());
// Expected output: "foo-1"
구문
super([arguments]) // ë¶ëª¨ ìì±ì í¸ì¶.
super.propertyOnParent
super[expression]
ì¤ëª
super
í¤ìëë "í¨ì í¸ì¶"(super(...args)
) ëë "ìì± ì¡°í"(super.prop
ì super[expr]
)ì ë ê°ì§ ë°©ë²ì¼ë¡ ì¬ì©í ì ììµëë¤.
ì°¸ê³ :
super
ë í¤ìëì´ë©° í¹ìí 구문 구조ì ëë¤.super
ë íë¡í íì ê°ì²´ë¥¼ ê°ë¦¬í¤ë ë³ìê° ìëëë¤.super
ì체를 ì½ì¼ë ¤ë ìëëSyntaxError
ì ëë¤.const child = { myParent() { console.log(super); // SyntaxError: 'super' keyword unexpected here }, };
íì í´ëì¤(extends
를 ì¬ì©)ì ìì±ì 본문ìì super
í¤ìëë "í¨ì í¸ì¶"(super(...args)
)ë¡ ëíë ì ìì¼ë©° this
í¤ìëê° ì¬ì©ë기 ì ê³¼ ìì±ìê° ë°íë기 ì ì í¸ì¶ëì´ì¼ í©ëë¤. ë¶ëª¨ í´ëì¤ì ìì±ì를 í¸ì¶íê³ ë¶ëª¨ í´ëì¤ì í¼ë¸ë¦ íë를 ë°ì¸ë©í í íì í´ëì¤ì ìì±ìë this
를 ì¶ê°ë¡ ì ê·¼íê³ ìì í ì ììµëë¤.
"ìì± ì¡°í" íìì ê°ì²´ 리í°ë´ ëë í´ëì¤ì [[Prototype]] ë©ìëì ìì±ì ì ê·¼íë ë° ì¬ì©í ì ììµëë¤. í´ëì¤ ë³¸ë¬¸ ë´ìì super
ì 참조ë ì¤í ë§¥ë½ì´ ì¸ì¤í´ì¤ ìì±ì¸ì§ í´ëì¤ ì´ê¸°íì¸ì§ì ë°ë¼ ìí¼í´ëì¤ì ìì±ì ìì²´ ì´ê±°ë ìì±ìì prototype
ì´ ë ì ììµëë¤. ìì¸í ë´ì©ì ìì 구íì 참조íì¸ì.
super
ì 참조ë ë©ìëê° í¸ì¶ë ê°ì²´ê° ìëë¼ super
ê° ì ì¸ë í´ëì¤ ëë ê°ì²´ 리í°ë´ì ìí´ ê²°ì ë©ëë¤. ë°ë¼ì ë©ìë를 ë°ì¸ë© í´ì íê±°ë ë¤ì ë°ì¸ë©í´ë ë©ìëì super
참조ë ë³ê²½ëì§ ììµëë¤.(this
ì 참조ë ë³ê²½ëì§ë§ì.) ë©ìëê° í´ë¡ì 를 ìì±íë í´ëì¤ ëë ê°ì²´ 리í°ë´ ë²ìì ë³ìë¡ super
를 ë³¼ ì ììµëë¤. (ê·¸ë¬ë ììì ì¤ëª
í ê²ì²ë¼ ì¤ì ë¡ë ë³ìê° ìëë¼ë ì ì ì ìíì¸ì.)
super
를 íµí´ ìì±ì ì¤ì í ë, ìì±ì this
ì ëì ì¤ì ë©ëë¤.
ì´ ì½ë ì¤ëí«ì í´ëì¤ ìí (ë¼ì´ë¸ ë°ëª¨)ìì ê°ì ¸ììµëë¤. ì¬ê¸°ì super()
ë Rectangle
ê³¼ Square
ì¬ì´ì ê³µíµì ì¸ ìì±ì ë¶ë¶ì ì¤ë³µì í¼í기 ìí´ í¸ì¶ë©ëë¤.
class Rectangle {
constructor(height, width) {
this.name = "Rectangle";
this.height = height;
this.width = width;
}
sayName() {
console.log(`Hi, I am a ${this.name}.`);
}
get area() {
return this.height * this.width;
}
set area(value) {
this._area = value;
}
}
class Square extends Rectangle {
constructor(length) {
this.height; // ReferenceError, superê° ë¨¼ì í¸ì¶ëì´ì¼ í©ëë¤!
// ì¬ê¸°ì Rectangleì ëë¹ì ëì´ì ëí´ ì ê³µë
// length를 ì¬ì©íì¬ ë¶ëª¨ í´ëì¤ì ìì±ì를 í¸ì¶í©ëë¤.
super(length, length);
// ì°¸ê³ : íì í´ëì¤ìì 'this'를 ì¬ì©íë ¤ë©´
// 먼ì super()를 í¸ì¶í´ì¼ í©ëë¤. ì´ë¥¼ ìëµíë©´ 참조 ì¤ë¥ê° ë°ìí©ëë¤.
this.name = "Square";
}
}
super를 í¸ì¶íë ì ì ë©ìë
ì ì ë©ìëììë super를 í¸ì¶í ì ììµëë¤.
class Rectangle {
static logNbSides() {
return "I have 4 sides";
}
}
class Square extends Rectangle {
static logDescription() {
return `${super.logNbSides()} which are all equal`;
}
}
Square.logDescription(); // 'I have 4 sides which are all equal'
í´ëì¤ íë ì ì¸ìì super ì ê·¼
super
ë í´ëì¤ íë ì´ê¸°í ì¤ìë ì ê·¼í ì ììµëë¤. super
ì 참조ë íì¬ íëê° ì¸ì¤í´ì¤ íëì¸ì§ ì ì íëì¸ì§ì ë°ë¼ ë¤ë¦
ëë¤.
class Base {
static baseStaticField = 90;
baseMethod() {
return 10;
}
}
class Extended extends Base {
extendedField = super.baseMethod(); // 10
static extendedStaticField = super.baseStaticField; // 90
}
ì¸ì¤í´ì¤ íëë ìì±ìì prototype
ëì ì¸ì¤í´ì¤ì ì¤ì ëë¯ë¡ super
를 ì¬ì©íì¬ ìí¼í´ëì¤ì ì¸ì¤í´ì¤ íëì ì ê·¼í ì ììµëë¤.
class Base {
baseField = 10;
}
class Extended extends Base {
extendedField = super.baseField; // undefined
}
ì¬ê¸°ì baseField
ë Base.prototype
ì´ ìëë¼ Base
ì¸ì¤í´ì¤ì ìì²´ ìì±ì¼ë¡ ì ìë기 ë문ì 10ì´ ìë undefined
ì
ëë¤. ì´ ë§¥ë½ìì super
ë Extended.prototype
ì [[Prototype]]ì´ë¯ë¡ Base.prototype
ì ìì±ë§ ì¡°íí ì ììµëë¤.
delete
ì°ì°ìì super.prop
ëë super[expr]
를 ì¬ì©íì¬ ë¶ëª¨ í´ëì¤ì ìì±ì ìì í ì ììµëë¤. ìì íë©´ ReferenceError
ê° ë°ìí©ëë¤.
class Base {
foo() {}
}
class Derived extends Base {
delete() {
delete super.foo; // ì´ë ê² íë©´ ìë©ëë¤.
}
}
new Derived().delete(); // ReferenceError: invalid delete involving 'super'.
ê°ì²´ 리í°ë´ìì super.prop ì¬ì©
superë ê°ì²´ ì´ê¸°ì í기ë²ììë ì¬ì©í ì ììµëë¤. ì´ ìì ë ë ê°ì²´ê° ë©ìë íë를 ì ìí©ëë¤. ë ë²ì§¸ ê°ì²´ìì super
ë 첫 ë²ì§¸ ê°ì²´ì ë©ìë를 í¸ì¶í©ëë¤. ì´ë obj2
ì íë¡í íì
ì obj1
ë¡ ì¤ì í ì ìë Object.setPrototypeOf()
ì ëìì¼ë¡ ìëíë¯ë¡ super
ë obj1
ìì method1
ì ì°¾ì ì ììµëë¤.
const obj1 = {
method1() {
console.log("method 1");
},
};
const obj2 = {
method2() {
super.method1();
},
};
Object.setPrototypeOf(obj2, obj1);
obj2.method2(); // "method 1"ì´ ë¡ê·¸ë©ëë¤.
super.propì ì½ë ë©ìëë ë¤ë¥¸ ê°ì²´ì ë°ì¸ë©ë ë ë¤ë¥´ê² ëìíì§ ììµëë¤
super.x
ì ì ê·¼íë ê²ì Reflect.get(Object.getPrototypeOf(objectLiteral), "x", this)
ì ê°ì´ ëìí©ëë¤. ì¦, ìì±ì íì ê°ì²´ 리í°ë´/í´ëì¤ ì ì¸ì íë¡í íì
ìì ê²ìëê³ ë©ìë를 ë°ì¸ë© í´ì íê³ ë¤ì ë°ì¸ë©í´ë super
ì 참조ë ë³ê²½ëì§ ììµëë¤.
class Base {
baseGetX() {
return 1;
}
}
class Extended extends Base {
getX() {
return super.baseGetX();
}
}
const e = new Extended();
console.log(e.getX()); // 1
const { getX } = e;
console.log(getX()); // 1
ê°ì²´ 리í°ë´ììë ëì¼íê² ëìí©ëë¤.
const parent1 = { prop: 1 };
const parent2 = { prop: 2 };
const child = {
myParent() {
console.log(super.prop);
},
};
Object.setPrototypeOf(child, parent1);
child.myParent(); // "1"ì´ ë¡ê·¸ë©ëë¤.
const myParent = child.myParent;
myParent(); // ì¬ì í "1"ì´ ë¡ê·¸ë©ëë¤.
const anotherChild = { __proto__: parent2, myParent };
anotherChild.myParent(); // ì¬ì í "1"ì´ ë¡ê·¸ë©ëë¤.
ì ì²´ ìì ì²´ì¸ì ì¬ì¤ì í´ì¼ë§ super
ì ì°¸ì¡°ê° ë³ê²½ë©ëë¤.
class Base {
baseGetX() {
return 1;
}
static staticBaseGetX() {
return 3;
}
}
class AnotherBase {
baseGetX() {
return 2;
}
static staticBaseGetX() {
return 4;
}
}
class Extended extends Base {
getX() {
return super.baseGetX();
}
static staticGetX() {
return super.staticBaseGetX();
}
}
const e = new Extended();
// ì¸ì¤í´ì¤ ìì ì¬ì¤ì
Object.setPrototypeOf(Extended.prototype, AnotherBase.prototype);
console.log(e.getX()); // íë¡í íì
ì²´ì¸ì´ ë³ê²½ëì기 ë문ì "1" ëì "2"ê° ë¡ê·¸ë©ëë¤.
console.log(Extended.staticGetX()); // ì ì ë¶ë¶ì ìì§ ìì íì§ ìì기 ë문ì, ì¬ì í "3"ì´ ë¡ê·¸ë©ëë¤.
// ì ì ìì ì¬ì¤ì
Object.setPrototypeOf(Extended, AnotherBase);
console.log(Extended.staticGetX()); // ì´ì "4"ê° ë¡ê·¸ë©ëë¤.
superìì ë©ìë í¸ì¶
super.prop
ì í¨ìë¡ í¸ì¶í ë prop
í¨ì ë´ë¶ì this
ê°ì super
ê° ê°ë¦¬í¤ë ê°ì²´ê° ìëë¼ íì¬ this
ì
ëë¤. ì를 ë¤ì´, super.getName()
í¸ì¶ì ì½ëê° Base.getName()
ê³¼ ëì¼í ê²ì²ë¼ ë³´ì´ì§ë§ "Extended"
를 ë¡ê·¸í©ëë¤.
class Base {
static getName() {
console.log(this.name);
}
}
class Extended extends Base {
static getName() {
super.getName();
}
}
Extended.getName(); // "Extended" ë¡ê·¸
ì´ë ì ì íë¼ì´ë¹ ìì±ê³¼ ìí¸ ìì©í ë í¹í ì¤ìí©ëë¤.
super.propì ì¤ì íë©´ this ìì±ì´ ëì ì¤ì ë©ëë¤super.x = 1
ê³¼ ê°ì super
ì ìì±ì ì¤ì íë ê²ì Reflect.set(Object.getPrototypeOf(objectLiteral), "x", 1, this)
ì ê°ì´ ëìí©ëë¤. ì´ë super
를 ë¨ìí "íë¡í íì
ê°ì²´ì 참조"ë¡ ì´í´íë ê²ì íê³ê° ìë ì´ì ì¤ íëì
ëë¤. ì¤ì ë¡ë this
ì ëí ìì±ì ì¤ì í기 ë문ì´ì£ .
class A {}
class B extends A {
setX() {
super.x = 1;
}
}
const b = new B();
b.setX();
console.log(b); // B { x: 1 }
console.log(Object.hasOwn(b, "x")); // true
super.x = 1
ì A.prototype
ìì x
ì ìì± ì¤ëª
ì를 ì°¾ìµëë¤(ê·¸ë¦¬ê³ ê±°ê¸°ì ì ìë setter를 í¸ì¶íì£ .). íì§ë§ this
ê°ì ì´ ë§¥ë½ìì b
ë¡ ì¤ì ë©ëë¤. target
ê³¼ receiver
ê° ë¤ë¥¸ ê²½ì°ì ëí ìì¸í ë´ì©ì Reflect.set
ì 참조íì¸ì.
ì¦, super.prop
를 getíë ë©ìëë ì¼ë°ì ì¼ë¡ ì´ ë§¥ë½ì ë³ê²½ì ìí¥ì ë°ì§ ìì§ë§ super.prop
를 setíë ë©ìëë ìí¥ì ë°ì ì ììµëë¤.
/* ìì ëì¼í ì ì¸ ì¬ì¬ì© */
const b2 = new B();
b2.setX.call(null); // TypeError: Cannot assign to read only property 'x' of object 'null'
ê·¸ë¬ë super.x = 1
ì ì¬ì í íë¡í íì
ê°ì²´ì ìì± ì¤ëª
ì를 참조íë¯ë¡, ì¸ ì ìë ìì±ì ë¤ì ìì±í ì ìì¼ë©° setterê° í¸ì¶ë©ëë¤.
class X {
constructor() {
// ì¸ ì ìë ìì± ìì±
Object.defineProperty(this, "prop", {
configurable: true,
writable: false,
value: 1,
});
}
}
class Y extends X {
constructor() {
super();
}
foo() {
super.prop = 2; // ê°ì ë®ì´ì¸ ì ììµëë¤.
}
}
const y = new Y();
y.foo(); // TypeError: "prop" is read-only
console.log(y.prop); // 1
ëª
ì¸ì ë¸ë¼ì°ì í¸íì± ê°ì´ 보기
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4