From 3ac7b3d6c977d427ec49eb0999c1de54403743a7 Mon Sep 17 00:00:00 2001 From: Masahiro FUJIMOTO Date: Thu, 8 Apr 2021 00:31:47 +0900 Subject: JavaScript/Reference/Classes/static を更新 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 2021/04/06 の英語版に同期 (this キーワードのリンク切れを直すついで) --- .../javascript/reference/classes/static/index.html | 94 +++++++++++----------- 1 file changed, 46 insertions(+), 48 deletions(-) (limited to 'files/ja') diff --git a/files/ja/web/javascript/reference/classes/static/index.html b/files/ja/web/javascript/reference/classes/static/index.html index c743df4aff..f9fb446e12 100644 --- a/files/ja/web/javascript/reference/classes/static/index.html +++ b/files/ja/web/javascript/reference/classes/static/index.html @@ -2,11 +2,11 @@ title: static slug: Web/JavaScript/Reference/Classes/static tags: - - Classes - - ECMAScript 2015 - - JavaScript - - Language feature - - Static +- Classes +- ECMAScript 2015 +- JavaScript +- Language feature +- Static translation_of: Web/JavaScript/Reference/Classes/static ---
{{jsSidebar("Classes")}}
@@ -15,17 +15,16 @@ translation_of: Web/JavaScript/Reference/Classes/static
{{EmbedInteractiveExample("pages/js/classes-static.html")}}
- -

構文

+

構文

-
static methodName() { ... }
+
static methodName() { ... }
 static propertyName [= value];
 
-

+

-

クラスでの静的メンバーの使用

+

クラスでの静的メンバーの使用

次の例はいくつかのことを説明しています。

@@ -35,42 +34,43 @@ static propertyName [= value];
  • 静的メンバーがどう呼び出せて、どう呼び出せないか
  • -
    class Triple {
    -  static customName = 'Tripler';
    -  static description = 'I triple any number you provide';
    -  static triple(n = 1) {
    +
    class Triple {
    +  static customName = 'Tripler';
    +  static description = 'I triple any number you provide';
    +  static calculate(n = 1) {
         return n * 3;
       }
     }
     
    -class BiggerTriple extends Triple {
    -  static longDescription;
    +class SquaredTriple extends Triple {
    +  static longDescription;
       static description = 'I square the triple of any number you provide';
    -  static triple(n) {
    -    return super.triple(n) * super.triple(n);
    +  static calculate(n) {
    +    return super.calculate(n) * super.calculate(n);
       }
     }
     
    -console.log(Triple.description);   // 'I triple any number you provide'
    -console.log(Triple.triple());      // 3
    -console.log(Triple.triple(6));     // 18
    +console.log(Triple.description);            // 'I triple any number you provide'
    +console.log(Triple.calculate());            // 3
    +console.log(Triple.calculate(6));           // 18
     
    -var tp = new Triple();
    +const tp = new Triple();
     
    -console.log(BiggerTriple.triple(3));        // 81 (not affected by parent's instantiation)
    -console.log(BiggerTriple.description);      // 'I square the triple of any number you provide'
    -console.log(BiggerTriple.longDescription);  // undefined
    -console.log(BiggerTriple.customName);       // 'Tripler'
    +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'
     
    -console.log(tp.triple());         // 'tp.triple is not a function'.
    +// This throws because calculate() is a static member, not an instance member.
    +console.log(tp.calculate());                // 'tp.calculate is not a function'
     
    -

    静的メンバーの別な静的メソッドからの呼び出し

    +

    静的メンバーの別な静的メソッドからの呼び出し

    同じクラス内の静的メソッドまたはプロパティを静的メソッドから呼び出すには、 this キーワードを使います。

    -
    class StaticMethodCall {
    -  static staticProperty = 'static property';
    +
    class StaticMethodCall {
    +  static staticProperty = 'static property';
       static staticMethod() {
         return 'Static method and ' + this.staticProperty + ' has been called';
       }
    @@ -84,11 +84,11 @@ StaticMethodCall.staticMethod();
     StaticMethodCall.anotherStaticMethod();
     // 'Static method and static property has been called from another static method'
    -

    クラスのコンストラクターや他のメソッドからの静的メソッドの呼び出し

    +

    クラスのコンストラクターや他のメソッドからの静的メソッドの呼び出し

    -

    静的メソッドは静的ではないメソッドの {{jsxref("this")}} キーワードを使用して直接アクセスすることができません。呼び出すにはクラス名を使用して クラス名.静的メソッド名() / クラス名.静的プロパティ名 のようにするか、 constructor プロパティのメソッドとして this.constructor.静的メソッド名() / this.constructor.静的プロパティ名 のようにしてください。

    +

    静的メソッドは静的ではないメソッドの {{JSxRef("Operators/this", "this")}} キーワードを使用して直接アクセスすることができません。呼び出すにはクラス名を使用して クラス名.静的メソッド名() / クラス名.静的プロパティ名 のようにするか、 constructor プロパティのメソッドとして this.constructor.静的メソッド名() / this.constructor.静的プロパティ名 のようにしてください。

    -
    class StaticMethodCall {
    +
    class StaticMethodCall {
       constructor() {
         console.log(StaticMethodCall.staticProperty); // 'static property'
         console.log(this.constructor.staticProperty); // 'static property'
    @@ -102,28 +102,26 @@ StaticMethodCall.anotherStaticMethod();
       }
     }
    -

    仕様書

    +

    仕様書

    - - - - - - - - - - + + + + + + + + + +
    仕様書
    {{SpecName('ESDraft', '#sec-class-definitions', 'Class definitions')}}
    仕様書
    {{SpecName('ESDraft', '#sec-class-definitions', 'Class definitions')}}
    -

    ブラウザーの互換性

    - - +

    ブラウザーの互換性

    {{Compat("javascript.classes.static")}}

    -

    関連情報

    +

    関連情報