| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Classes/static | [Back] [Original] |
Get to know MDN better
class ClassWithStaticMethod {
static staticProperty = "someValue";
static staticMethod() {
return "";
}
static {
console.log("");
}
}
console.log(ClassWithStaticMethod.staticProperty);
// : "someValue"
console.log(ClassWithStaticMethod.staticMethod());
// : ""
class ClassWithStatic {
static staticField;
static staticFieldWithInitializer = value;
static staticMethod() {
//
}
}
prototype constructor static [[DefineOwnProperty]] Object.defineProperty()
this this ReferenceError await yield
undefined
class ClassWithStaticField {
static staticField;
static staticFieldWithInitializer = "";
}
class SubclassWithStaticField extends ClassWithStaticField {
static subStaticField = "";
}
console.log(Object.hasOwn(ClassWithStaticField, "staticField")); // true
console.log(ClassWithStaticField.staticField); // undefined
console.log(ClassWithStaticField.staticFieldWithInitializer); // ""
console.log(SubclassWithStaticField.staticFieldWithInitializer); // ""
console.log(SubclassWithStaticField.subStaticField); // ""
class ClassWithStaticField {
static baseStaticField = "";
static anotherBaseStaticField = this.baseStaticField;
static baseStaticMethod() {
return "";
}
}
class SubClassWithStaticField extends ClassWithStaticField {
static subStaticField = super.baseStaticMethod();
}
console.log(ClassWithStaticField.anotherBaseStaticField); // ""
console.log(SubClassWithStaticField.subStaticField); // ""
class Triple {
static customName = "Tripler";
static description = "I triple any number you provide";
static calculate(n = 1) {
return n * 3;
}
}
class SquaredTriple extends Triple {
static longDescription;
static description = "I square the triple of any number you provide";
static calculate(n) {
return super.calculate(n) * super.calculate(n);
}
}
console.log(Triple.description); // 'I triple any number you provide'
console.log(Triple.calculate()); // 3
console.log(Triple.calculate(6)); // 18
const tp = new Triple();
console.log(SquaredTriple.calculate(3)); // 81 (not affected by parent's instantiation)
console.log(SquaredTriple.description); // 'I square the triple of any number you provide'
console.log(SquaredTriple.longDescription); // undefined
console.log(SquaredTriple.customName); // 'Tripler'
// calculate()
console.log(tp.calculate()); // 'tp.calculate is not a function'
class StaticMethodCall {
static staticProperty = "static property";
static staticMethod() {
return `Static method and ${this.staticProperty} has been called`;
}
static anotherStaticMethod() {
return `${this.staticMethod()} from another static method`;
}
}
StaticMethodCall.staticMethod();
// 'Static method and static property has been called'
StaticMethodCall.anotherStaticMethod();
// 'Static method and static property has been called from another static method'
this .() / . constructor this.constructor.() / this.constructor.
class StaticMethodCall {
constructor() {
console.log(StaticMethodCall.staticProperty); // 'static property'
console.log(this.constructor.staticProperty); // 'static property'
console.log(StaticMethodCall.staticMethod()); // ''
console.log(this.constructor.staticMethod()); // ''
}
static staticProperty = "static property";
static staticMethod() {
return "";
}
}
| ECMAScript 2027 LanguageSpecification # sec-class-definitions |
| Web Proxy Viewer | New URL | Original Page |