Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. JavaScript
  3. Reference
  4. JavaScript error reference
  5. SyntaxError: use of super property/member accesses only valid within methods or eval code within methods

SyntaxError: use of super property/member accesses only valid within methods or eval code within methods

The JavaScript exception "use of super property/member accesses only valid within methods or eval code within methods" occurs when thesuper.x orsuper[x] syntax is used outside of amethod.

Message

SyntaxError: 'super' keyword unexpected here (V8-based)SyntaxError: use of super property accesses only valid within methods or eval code within methods (Firefox)SyntaxError: super is not valid in this context. (Safari)

Error type

SyntaxError

What went wrong?

Thesuper.x syntax is used to access properties on the prototype of the current object. It can be used in methods of bothobject literals andclasses,field initializers, andstatic initialization blocks, but not in other contexts.

Examples

Invalid cases

You can't usesuper.x outside of a method in an object:

js
const obj = {  __proto__: { x: 1 },  x: super.x, // SyntaxError: use of super property accesses only valid within methods or eval code within methods};

You can't usesuper.x in a function, even if that function has the effect of being a method:

js
function getX() {  return super.x; // SyntaxError: use of super property accesses only valid within methods or eval code within methods}const obj = {  getX,  getX2: function () {    return super.x; // SyntaxError: use of super property accesses only valid within methods or eval code within methods  },};class Derived extends Base {  getX = () => super.x;}

Valid cases

You can usesuper.x in a method:

js
class Base {  x = 1;}class Derived extends Base {  getX() {    return super.x;  }}

You can usesuper.x in a field initializer:

js
class Derived extends Base {  x = super.x;}

You can usesuper.x in object methods too:

js
const obj = {  __proto__: { x: 1 },  getX() {    return super.x;  },};

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp