Constructor shorthand
Không thể đặt access modifier cho tham số của method, nhưng constructor là trường hợp đặc biệt.
Khi khai báo access modifier cho tham số, ý nghĩa như sau:
| Access modifier | Mô tả |
|---|---|
| (không khai báo) | Chỉ có thể truy cập trong method constructor |
| public | Có thể truy cập từ class hiện tại, class kế thừa và class đã instance hóa |
| protected | Có thể truy cập từ class hiện tại và class kế thừa |
| private | Chỉ có thể truy cập từ class hiện tại |
Định nghĩa hai class ConstructorInAccessModifier và ConstructorOutAccessModifier.
Sự khác biệt duy nhất giữa hai class là có khai báo access modifier trong constructor hay không, chức năng hoàn toàn giống nhau.
example.tstsclassConstructorInAccessModifier {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.tstsclassConstructorInAccessModifier {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 });}}
Xem file JavaScript sau khi compile có thể xác nhận cả hai có chức năng giống hệt nhau.
example.jsjsclassConstructorInAccessModifier {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.jsjsclassConstructorInAccessModifier {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 });}}
Khi viết TypeScript, chức năng scope của mỗi access modifier có hiệu lực, nên property có thể truy cập từ instance chỉ là arg1 được khai báo public.
example.tstsconstInAccess = 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.tstsconstInAccess = 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
Tức là access modifier của tham số constructor chỉ đơn giản là cách viết tắt cho việc khai báo property.