Quellcodebibliothek Statistik Leitseite products/sources/formale Sprachen/C/Firefox/js/src/jit-test/tests/class/   (Browser von der Mozilla Stiftung Version 136.0.1©)  Datei vom 10.2.2025 mit Größe 2 kB image not shown  

Quelle  class-static-01.js   Sprache: JAVA

 
// |jit-test| 
load(libdir + "asserts.js");

// Parse
class A {
  static { }
};

// Run
var ran = false;
class B {
  static {
    ran = true;
  }
};

assertEq(ran, true);

// Can access static assigned before.
var res;
class C {
  static x = 10;
  static {
    res = this.x;
  }
};

assertEq(res, 10);

// Multiple static blocks
class D {
  static { }
  static { }
};

class D1 {
  static { } static { }
};

// Blocks run in order.
class E {
  static {
    res = 10;
  }
  static {
    assertEq(res, 10);
    res = 14;
  }
}

assertEq(res, 14);


// Can use static to access private state.
let accessor;
class F {
  #x = 10
  static {
    accessor = (o) => o.#x;
  }
}

let f = new F;
assertEq(accessor(f), 10);

class G {
  static {
    this.a = 10;
  }
}
assertEq(G.a, 10);

// Can use the super-keyword to access a static method in super class.
class H {
  static a() { return 101; }
}

class I extends H {
  static {
    res = super.a();
  }
}

assertEq(res, 101);

// Can add a property to a class from within a static
class J {
  static {
    this.x = 12;
  }
}

assertEq(J.x, 12);


function assertThrowsSyntaxError(str) {
  assertThrowsInstanceOf(() => eval(str), SyntaxError);       // Direct Eval
  assertThrowsInstanceOf(() => (1, eval)(str), SyntaxError);  // Indirect Eval
  assertThrowsInstanceOf(() => Function(str), SyntaxError);   // Function
}

assertThrowsSyntaxError(`
class X {
  static {
    arguments;
  }
}
`)


assertThrowsSyntaxError(`
class X extends class {} {
  static {
    super(); // can't call super in a static block
  }
}
`)

assertThrowsSyntaxError(`
class X {
  static {
    return 10; // Can't return in a static block
  }
}
`)

assertThrowsSyntaxError(`
class X {
  static {
    await 10; // Can't await in a static block
  }
}
`)

// Can't await inside a static block, even if we're inside an async function.
//
// The error message here is not good `SyntaxError: await is a reserved identifier`,
// but can be fixed later. s
assertThrowsSyntaxError(`
async function f() {
  class X {
    static {
      await 10; // Can't await in a static block
    }
  }
}
`)


assertThrowsSyntaxError(`
class X {
  static {
    yield 10; // Can't yield in a static block
  }
}
`)


assertThrowsSyntaxError(`
function* gen() {
  class X {
    static {
      yield 10; // Can't yield in a static block, even inside a generator
    }
  }
}
`)

// Incomplete should throw a sensible error.
assertThrowsSyntaxError(`
class A { static { this.x
`)

Messung V0.5
C=95 H=89 G=91

¤ Dauer der Verarbeitung: 0.1 Sekunden  (vorverarbeitet)  ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

Beweissystem der NASA

Beweissystem Isabelle

NIST Cobol Testsuite

Cephes Mathematical Library

Wiener Entwicklungsmethode

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.