[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Operators/super [Back]  [Original]

super - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

super

Baseline

20163

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;
  }

  // 
  getNameSeparator() {
    return "/";
  }

  getFullName() {
    return this.name + super.getNameSeparator() + this.index;
  }
}

const firstFooBar = new FooBar("foo", 1);

console.log(firstFooBar.name);
// : "foo"

console.log(firstFooBar.getFullName());
// : "foo-1"

js
super()
super(arg1)
super(arg1, arg2)
super(arg1, arg2, /* , */ argN)

super.propertyOnParent
super[expression]

super super(...args)super.prop super[expr] 2

: super super super SyntaxError

js
const child = {
  myParent() {
    console.log(super); // SyntaxError: 'super' keyword unexpected here
  },
};

extends super super(...args) this this

[[Prototype]] super prototype

supersupersuperthissuper

super this

super

() super() Rectangle Square

js
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) {
    // 
    // Rectangle  
    super(length, length);

    // : 'this'  super() 
    //  ReferenceError 
    this.name = "Square";
  }
}

super

super

js
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

js
class Base {
  static baseStaticField = 90;
  baseMethod() {
    return 10;
  }
}

class Extended extends Base {
  extendedField = super.baseMethod(); // 10
  static extendedStaticField = super.baseStaticField; // 90
}

prototype super

js
class Base {
  baseField = 10;
}

class Extended extends Base {
  extendedField = super.baseField; // undefined
}

extendedField 10 undefined baseField Base.prototype Base super Extended.prototype [[Prototype]] Base.prototype

super

delete super.propsuper[expr] ReferenceError

js
class Base {
  foo() {}
}
class Derived extends Base {
  delete() {
    delete super.foo; // this is bad
  }
}

new Derived().delete(); // ReferenceError: invalid delete involving 'super'.

super.prop

super / 2 2 super Object.setPrototypeOf() obj2 obj1 super method1 obj1

js
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

js
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

js
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

js
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 this super super.getName() Base.getName() "Extended"

js
class Base {
  static getName() {
    console.log(this.name);
  }
}

class Extended extends Base {
  static getName() {
    super.getName();
  }
}

Extended.getName(); // "Extended" 

super.prop this

super super.x = 1 Reflect.set(Object.getPrototypeOf(objectLiteral), "x", 1, this) super this

js
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 this this bReflect.set target receiver

super.prop this super.prop

js
/*  */

const b2 = new B();
b2.setX.call(null); // TypeError: Cannot assign to read only property 'x' of object 'null'

super.x = 1

js
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" 
console.log(y.prop); // 1

ECMAScript 2027 LanguageSpecification
# sec-super-keyword


Web Proxy Viewer  |  New URL  |  Original Page