constructor shorthand
メソッドの引数にはアクセス修飾子を設定することはできませんがコンストラクタは特別です。
引数に対してアクセス修飾子を宣言した場合はこのような意味になります。
アクセス修飾子 | 説明 |
---|---|
(宣言なし) | constructorメソッド内のみアクセス可能 |
public | 自身のクラス内、継承クラス、インスタンス化されたクラスのどれからでもアクセス可能 |
protected | 自身のクラス、継承クラスからアクセス可能 |
private | 自身のクラスのみアクセス可能 |
ConstructorInAccessModifier
クラスとConstructorOutAccessModifier
クラスのふたつを定義しました。
ふたつのクラスの違いはコンストラクタにアクセス修飾子を定義しているかどうかだけで機能はまったく同じです。
example.tsts
classConstructorInAccessModifier {constructor(arg0 : number,publicarg1 : number,protectedarg2 : number,privatearg3 : number) {console .log ({arg0 ,arg1 ,arg2 ,arg3 });}}classConstructorOutAccessModifier {publicarg1 : number;protectedarg2 : number;privatearg3 : number;constructor(arg0 : number,arg1 : number,arg2 : number,arg3 : number) {this.arg1 =arg1 ;this.arg2 =arg2 ;this.arg3 =arg3 ;console .log ({arg0 ,arg1 ,arg2 ,arg3 });}}
example.tsts
classConstructorInAccessModifier {constructor(arg0 : number,publicarg1 : number,protectedarg2 : number,privatearg3 : number) {console .log ({arg0 ,arg1 ,arg2 ,arg3 });}}classConstructorOutAccessModifier {publicarg1 : number;protectedarg2 : number;privatearg3 : number;constructor(arg0 : number,arg1 : number,arg2 : number,arg3 : number) {this.arg1 =arg1 ;this.arg2 =arg2 ;this.arg3 =arg3 ;console .log ({arg0 ,arg1 ,arg2 ,arg3 });}}
コンパイル後のJavaScriptファイルを見てみると同一の機能を持つことが確認することができます。
example.jsjs
classConstructorInAccessModifier {constructor(arg0 ,arg1 ,arg2 ,arg3 ) {this.arg1 =arg1 ;this.arg2 =arg2 ;this.arg3 =arg3 ;console .log ({arg0 ,arg1 ,arg2 ,arg3 });}}classConstructorOutAccessModifier {constructor(arg0 ,arg1 ,arg2 ,arg3 ) {this.arg1 =arg1 ;this.arg2 =arg2 ;this.arg3 =arg3 ;console .log ({arg0 ,arg1 ,arg2 ,arg3 });}}
example.jsjs
classConstructorInAccessModifier {constructor(arg0 ,arg1 ,arg2 ,arg3 ) {this.arg1 =arg1 ;this.arg2 =arg2 ;this.arg3 =arg3 ;console .log ({arg0 ,arg1 ,arg2 ,arg3 });}}classConstructorOutAccessModifier {constructor(arg0 ,arg1 ,arg2 ,arg3 ) {this.arg1 =arg1 ;this.arg2 =arg2 ;this.arg3 =arg3 ;console .log ({arg0 ,arg1 ,arg2 ,arg3 });}}
TypeScriptで記述する際は各アクセス修飾子のスコープ機能が有効になるため、インスタンスからのアクセスが可能なプロパティはpublic
宣言されたarg1
のみが有効になります。
example.tsts
constInAccess = newConstructorInAccessModifier (1, 2, 3, 4);Property 'arg0' does not exist on type 'ConstructorInAccessModifier'.2339Property 'arg0' does not exist on type 'ConstructorInAccessModifier'.InAccess .; arg0 InAccess .arg1 ;Property 'arg2' is protected and only accessible within class 'ConstructorInAccessModifier' and its subclasses.2445Property 'arg2' is protected and only accessible within class 'ConstructorInAccessModifier' and its subclasses.InAccess .; arg2 Property 'arg3' is private and only accessible within class 'ConstructorInAccessModifier'.2341Property 'arg3' is private and only accessible within class 'ConstructorInAccessModifier'.InAccess .; arg3 constoutAccess = newConstructorOutAccessModifier (1, 2, 3, 4);Property 'arg0' does not exist on type 'ConstructorOutAccessModifier'.2339Property 'arg0' does not exist on type 'ConstructorOutAccessModifier'.outAccess .; arg0 outAccess .arg1 ;Property 'arg2' is protected and only accessible within class 'ConstructorOutAccessModifier' and its subclasses.2445Property 'arg2' is protected and only accessible within class 'ConstructorOutAccessModifier' and its subclasses.outAccess .; arg2 Property 'arg3' is private and only accessible within class 'ConstructorOutAccessModifier'.2341Property 'arg3' is private and only accessible within class 'ConstructorOutAccessModifier'.outAccess .; arg3
example.tsts
constInAccess = newConstructorInAccessModifier (1, 2, 3, 4);Property 'arg0' does not exist on type 'ConstructorInAccessModifier'.2339Property 'arg0' does not exist on type 'ConstructorInAccessModifier'.InAccess .; arg0 InAccess .arg1 ;Property 'arg2' is protected and only accessible within class 'ConstructorInAccessModifier' and its subclasses.2445Property 'arg2' is protected and only accessible within class 'ConstructorInAccessModifier' and its subclasses.InAccess .; arg2 Property 'arg3' is private and only accessible within class 'ConstructorInAccessModifier'.2341Property 'arg3' is private and only accessible within class 'ConstructorInAccessModifier'.InAccess .; arg3 constoutAccess = newConstructorOutAccessModifier (1, 2, 3, 4);Property 'arg0' does not exist on type 'ConstructorOutAccessModifier'.2339Property 'arg0' does not exist on type 'ConstructorOutAccessModifier'.outAccess .; arg0 outAccess .arg1 ;Property 'arg2' is protected and only accessible within class 'ConstructorOutAccessModifier' and its subclasses.2445Property 'arg2' is protected and only accessible within class 'ConstructorOutAccessModifier' and its subclasses.outAccess .; arg2 Property 'arg3' is private and only accessible within class 'ConstructorOutAccessModifier'.2341Property 'arg3' is private and only accessible within class 'ConstructorOutAccessModifier'.outAccess .; arg3
つまり、コンストラクタの引数のアクセス修飾子はプロパティ宣言の省略をしてくれるだけにすぎません。