クラス (class)
クラスはオブジェクトの雛形を定義したもので、JavaScriptとTypeScriptではclass構文を用いてクラスが定義できます。
javascriptclass Person {}
javascriptclass Person {}
クラスに対してnewキーワードを使うと、オブジェクトを生成できます。
javascriptconst person = new Person();
javascriptconst person = new Person();
このようにclassでクラスを定義し、newでインスタンスを生成するスタイルは、JavaやPHP、Rubyなどと使用感がよく似ています。
クラスの型注釈#
TypeScriptでは、クラスを定義するとクラス名と同じ名前の型が同時に定義されます。インスタンスを代入する変数に型注釈するには、クラス名を使います。
typescriptconst person: Person = new Person();
typescriptconst person: Person = new Person();