--- title: class slug: Web/JavaScript/Reference/Statements/class tags: - Classes - Declaration - ECMAScript6 - JavaScript - Reference - クラス - 宣言 translation_of: Web/JavaScript/Reference/Statements/class --- <div>{{jsSidebar("Statements")}}</div> <p><strong>クラス宣言</strong>は、プロトタイプベースの継承を使って、指定された名前の新しいクラスを作成します。</p> <div>{{EmbedInteractiveExample("pages/js/statement-class.html")}}</div> <div class="hidden">このデモのソースファイルは GitHub リポジトリに格納されています。デモプロジェクトに協力したい場合は、 <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> をクローンしてプルリクエストを送信してください。</div> <div class="noinclude"> <p>{{jsxref("Operators/class", "クラス式", "", 1)}}を使ってクラスを定義することもできます。しかし、クラス式と異なり、クラス宣言は既存のクラスを再宣言することができず、再宣言しようとすると {{jsxref("SyntaxError")}} が発生します。</p> </div> <h2 id="Syntax" name="Syntax">構文</h2> <pre class="syntaxbox">class <var>name</var> [extends <var>otherName</var>] { // クラス本体 }</pre> <h2 id="Description" name="Description">解説</h2> <p>クラス式と同様、クラス宣言の内部は<a href="/ja/docs/Web/JavaScript/Reference/Strict_mode">厳格モード</a>で実行されます。 <code>constructor</code> メソッドは省略可能です。</p> <p>クラス宣言は{{Glossary("Hoisting", "巻き上げ")}}が行われません (<a href="/ja/docs/Web/JavaScript/Reference/Statements/function">関数宣言</a>とは異なります)。</p> <h2 id="Examples" name="Examples">例</h2> <h3 id="A_simple_class_declaration" name="A_simple_class_declaration">単純なクラス宣言</h3> <p>次の例では、はじめに <code>Polygon</code> という名前のクラスを定義し、次にそれを拡張して <code>Square</code> という名前のクラスを作成します。</p> <p>なお、コンストラクターで使われている <code>super()</code> は、コンストラクター内でのみ使えること、 <code>this</code> キーワードの使用<em>前</em>に呼び出さなくてはならないことに注意してください。</p> <pre class="brush: js">class Polygon { constructor(height, width) { this.name = 'Polygon'; this.height = height; this.width = width; } } class Square extends Polygon { constructor(length) { super(length, length); this.name = 'Square'; } }</pre> <h3 id="Attempting_to_declare_a_class_twice" name="Attempting_to_declare_a_class_twice">クラスを二度宣言する</h3> <p>クラス宣言を使って再度クラスを宣言すると、 {{jsxref("SyntaxError")}} が発生します。</p> <pre class="brush: js example-bad">class Foo {}; class Foo {}; // Uncaught SyntaxError: Identifier 'Foo' has already been declared </pre> <p>クラス式を使って事前にクラスを定義していたときも、同じエラーが発生します。</p> <pre class="brush: js example-bad">let Foo = class {}; class Foo {}; // Uncaught SyntaxError: Identifier 'Foo' has already been declared </pre> <h2 id="Specifications" name="Specifications">仕様書</h2> <table class="standard-table"> <thead> <tr> <th scope="col">仕様書</th> </tr> </thead> <tbody> <tr> <td>{{SpecName('ESDraft', '#sec-class-definitions', 'Class definitions')}}</td> </tr> </tbody> </table> <h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2> <div class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</div> <p>{{Compat("javascript.statements.class")}}</p> <h2 id="See_also" name="See_also">関連情報</h2> <ul> <li><a href="/ja/docs/Web/JavaScript/Reference/Statements/function"><code>function</code> 宣言</a></li> <li><a href="/ja/docs/Web/JavaScript/Reference/Operators/class"><code>class</code> 式</a></li> <li><a href="/ja/docs/Web/JavaScript/Reference/Classes">クラス</a></li> </ul>