Baseline Widely available
super ãã¼ã¯ã¼ãã¯ããªãã¸ã§ã¯ãã®è¦ªã®é¢æ°ãå¼ã³åºãããã«ä½¿ç¨ã§ãã¾ãã
super.prop
ããã³ super[expr]
å¼ã¯ãclass ã¨ãªãã¸ã§ã¯ããªãã©ã«ã®ä¸¡æ¹ã«ãããããããã¡ã½ããå®ç¾©ã§æå¹ã§ãã
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.functionOnParent([arguments]);
解説
ã³ã³ã¹ãã©ã¯ã¿ã¼ã§ä½¿ç¨ããå ´åãsuper
ãã¼ã¯ã¼ããåç¬ã§ç½®ããthis
ãã¼ã¯ã¼ãã使ãããåã«ä½¿ç¨ããå¿
è¦ãããã¾ããsuper
ãã¼ã¯ã¼ãã¯ã親ãªãã¸ã§ã¯ãã®é¢æ°ãå¼ã³åºãããã«ã使ç¨ã§ãã¾ãã
super
ã®ä½¿ç¨
ãã®ã³ã¼ãã¹ããããã¯ãã¯ã©ã¹ã®ä¾ (ã©ã¤ããã¢) ããã¨ã£ã¦ãã¾ãã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 ãå
ã«å¼ã³åºããªããã°ãªãã¾ããï¼
// length ã®å¤ã§è¦ªã¯ã©ã¹ã® constructor ãå¼ã³ã¾ãã
// Rectangle ã® width 㨠height ã«ãªãã¾ãã
super(length, length);
// Note: 'this' ã使ãåã« super() ãã³ã¼ã«ããªããã°ãªãã¾ããã
// ã§ãªã㨠reference error ã«ãªãã¾ãã
this.name = "Square";
}
}
éçã¡ã½ããã§ã® super ã®å¼ã³åºã
static ã¡ã½ããã§ã 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 ããããã£ãåé¤ããã¨ã¨ã©ã¼ãçºçãã
親ã¯ã©ã¹ã®ããããã£ãåé¤ããããã«ãdelete æ¼ç®å ã super.prop
ãsuper[expr]
ã使ããã¨ã¯ã§ãã¾ãããReferenceError
ãã¹ãã¼ããã¾ãã
class Base {
foo() {}
}
class Derived extends Base {
delete() {
delete super.foo; // this is bad
}
}
new Derived().delete(); // ReferenceError: invalid delete involving 'super'.
super.prop
ã¯æ¸ãè¾¼ã¿ä¸å¯è½ãªããããã£ã䏿¸ãã§ããªã
Object.defineProperty
ãªã©ã§æ¸ãè¾¼ã¿ä¸å¯ããããã£ãå®ç¾©ããå ´åãsuper
ã¯ããããã£ã®å¤ã䏿¸ãã§ãã¾ããã
class X {
constructor() {
Object.defineProperty(this, "prop", {
configurable: true,
writable: false,
value: 1,
});
}
}
class Y extends X {
constructor() {
super();
}
foo() {
super.prop = 2; // å¤ã䏿¸ãã§ããªã
}
}
var y = new Y();
y.foo(); // TypeError: "prop" ã¯èªã¿åãå°ç¨
console.log(y.prop); // 1
ãªãã¸ã§ã¯ããªãã©ã«å
ã§ã® super.prop
ã®ä½¿ç¨
super ã¯ãªãã¸ã§ã¯ãåæåå / ãªãã©ã«è¨æ³å
ã§ã使ç¨ã§ãã¾ãããã®ä¾ã§ã¯ã 2 ã¤ã®ãªãã¸ã§ã¯ããã¡ã½ãããå®ç¾©ãã¦ãã¾ãã 2 ã¤ç®ã®ãªãã¸ã§ã¯ãã®ä¸ã§ãsuper
ãæåã®ãªãã¸ã§ã¯ãã®ã¡ã½ãããå¼ã³åºãã¦ãã¾ãããã㯠Object.setPrototypeOf()
ã®å©ãã§åä½ãããã㯠obj2
ã®ãããã¿ã¤ãã obj1
ã«è¨å®ããã®ã§ãsuper
㯠method1
ã obj1
ä¸ã§è¦ã¤ãããã¨ãã§ãã¾ãã
var obj1 = {
method1() {
console.log("method 1");
},
};
var obj2 = {
method2() {
super.method1();
},
};
Object.setPrototypeOf(obj2, obj1);
obj2.method2(); // logs "method 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