TypeScript là một ngôn ngữ mã nguồn mở miễn phí hiện đang được phát triển và bảo trì bởi Microsoft. Nó là superset của JavaScript, với các bổ sung các tuỳ chọn kiểu tĩnh và lớp trên cơ sở lập trình hướng đối tượng cho ngôn ngữ này. Vì TypeScript là tập cha của JavaScript nên bất kì chương trình JavaScript nào đã có cũng đều là chương trình TypeScript hợp lệ. nhưng hầu hết các trình duyệt chưa hỗ trợ nên cần được biên dịch ra JavaScript để có thể hoạt động. Có 4 tính chất của một ngôn ngữ lập trình hướng đối tượng là tính bao đóng (encapsulation), tính kế thừa(inheritance), tính đa hình(polymorphism), tính trừu tượng(Abstraction) và TypeScript hỗ trợ cả 4 tính chất này một cách rõ ràng và ngắn gọn
1. Tính trừu tượng (Abstraction)
Lớp trừu tượng là lớp mà từ đó các lớp khác có thể kế thừa được. Không giống như interface, lớp trừu tượng có thể chứa các thuộc tính cũng như các hàm thực thi(các phương thức abstract thì chỉ khai báo chứ không thực thi). Trong TypeScript cũng sự dụng từ khóa abstract để định nghĩa các lớp và phương thức trừu tượng. Chúng ta cùng xét ví dụ ở dưới đây:
abstract class Employee{
private name:string;
private age:number;
private qualification:string;
constructor(name:string, age:number, qualification:string){
this.name = name;
this.age = age;
this.qualification = qualification;
}
abstract applyTheJob():void // phương thức abstract không chứa hàm thực thi
abstract mailCheck():void
getNameAge(){
console.log(this.name + this.age)
}
getQualification(){
console.log(this.qualification)
}
}
private name:string;
private age:number;
private qualification:string;
constructor(name:string, age:number, qualification:string){
this.name = name;
this.age = age;
this.qualification = qualification;
}
abstract applyTheJob():void // phương thức abstract không chứa hàm thực thi
abstract mailCheck():void
getNameAge(){
console.log(this.name + this.age)
}
getQualification(){
console.log(this.qualification)
}
}
Kế thừa từ lớp trừu tượng: các class được kế thừa phải khai báo đầy đủ các phương thức abstract của class cha
Abstraction (tính trừu tượng) và encapsulation (tính bao đóng chúng ta sẽ tìm hiểu ngay sau đây) là hai đặc điểm có liên quan nhau trong lập trình hướng đối tượng. Trừu tượng cho phép tạo ra các thông tin không được hiển thị rõ ràng và đóng gói cho phép một người lập trình thể hiện tính trừu tượng ở mức độ mong muốn.
class _employee extends Employee{
constructor(name:string, age:number, qualification:string){
super(name, age, qualification);
}
applyTheJob(){
console.log('Applying for web developer position')
}
mailCheck(){
console.log('Confirm mail before the interview')
}
}
constructor(name:string, age:number, qualification:string){
super(name, age, qualification);
}
applyTheJob(){
console.log('Applying for web developer position')
}
mailCheck(){
console.log('Confirm mail before the interview')
}
}
2. Tính bao đóng (Encapsulation):
Tính bao đóng là tính chất không cho phép cho người dùng hay đối tượng thay đổi dự liệu thành viên của đối tượng nội tại. Chỉ có các hàm thành viên của đối tượng mới có quyền thay đổi trạng thái nội tại đó. Các đối tượng khác muốn thay đổi cần phải truyền một thông điệp cho đối tượng. Việc quyết định có thay đổi hay không vẫn do đối tượng nội tại quyết định
Tính đóng gói là không cho phép bên ngoài biết được bên trong đối tượng có những gì hay logic xử lí như thế nào, nếu muốn thay đổi bên trong đối tượng thi phải được sự chấp nhận của đối tượng đó thông qua các mức độ truy cập private, protected, public
Tính đóng gói là không cho phép bên ngoài biết được bên trong đối tượng có những gì hay logic xử lí như thế nào, nếu muốn thay đổi bên trong đối tượng thi phải được sự chấp nhận của đối tượng đó thông qua các mức độ truy cập private, protected, public
Sau đây là một ví dụ về tính bao đóng:
class Employee{
private name:string;
private age:number;
private qualification:string;
constructor(name:string, age:number, qualification:string){
this.name = name;
this.age = age;
this.qualification = qualification;
}
public getInfo(){
console.log(`
Name: ${this.name};
age: ${this.age};
Qualification: ${this.qualification}
`);
}
}
let _employee = new Employee('NVA',24,'bachelor');
class ScutiCompany{ public member:number; public name:string; constructor(name:string){ this.name = name; } static getInfoEmployee(){ return _employee.getInfo(); } } ScutiCompany.getInfoEmployee();
Các thuộc tính trong class Employee để ở trạng thái private và chỉ có thể được lấy ra từ phương thức public getInfo(), như vậy đối tượng _employee sẽ được đóng gói. Từ đó class company có thể lấy được thông tin đối tượng employee từ phương thức getInfo()
3. Tính kế thừa (Inheritance)
Nhằm tránh việc khai báo lặp lại các phương thức và thuộc tính, thêm vào đó là thuận tiện trong việc bảo trì và nâng cấp code dễ dàng hơn và giải pháp là tính kế thừa. Tính kế thừa là tính chất rất quan trọng trong lập trình hướng đối tượng. Có thể thể hiện tính kế thừa trong TypeScript một cách dễ dàng qua từ khóa extends :class Company{
protected name:string;
protected member:number;
constructor(name:string, numberMember:number){
this.name = name;
this.member = numberMember;
}
public getInformation(){
console.log(`
Welcome ${this.name} <br/>
We have ${this.member} member
`);
}
}
class ITcompany extends Company{
public makeWebsite(nameProject:string){
console.log('We are making' + nameProject)
}
public makeProduct(nameProject:string){
console.log(`We are developing ${nameProject} our own product `)
}
}
let Scuti = new ITcompany('Scuti',15);
Scuti.getInformation();
Scuti.makeWebsite('OutSourcing_projects');
Scuti.makeProduct('CookStar');
Có thể dễ
dàng nhận thấy class ITcompany thừa kế từ class Company thông qua từ khóa
extends, từ đó các phương thức và thuộc
tính protected hay public đều được thế hiện ở class ITcompany
4. Tính đa hình (Polymorphism)
Tính đa
hình trong lập trình hướng đối tượng là sự thay đổi hành vi của phương thức ở mỗi đối tượng khác nhau. Đi đôi với tính kế thừa ta có tính đa hình cũng rất dễ
để thể hiện trong TypeScript. Ta cùng xét ví dụ quen thuộc sau sẽ thấy rõ ràng nhất tính đa hình trong OOP:
const PI = 3.14;
class Shape{
constructor(name:string){}
protected Perimeter(){}
protected Acreage(){}
}
class Circle extends Shape{
private radius:number;
constructor(name:string,radius:number){
super(name);
this.radius = radius;
}
Perimeter(){
let perimeter = 2*PI*this.radius;
console.log('Perimeter is:' + perimeter);
}
Acreage(){
let acreage = PI*this.radius*this.radius;
console.log('Acreage is:' + acreage);
}
}
Bây giờ tiếp
tục tạo thêm một class Square kế thừa từ Shape
class Square extends Shape{
private length:number;
private width:number;
constructor(name:string, length:number, width:number){
super(name);
this.length = length;
this.width = width;
}
Perimeter(){
let perimeter = (this.length + this.width)*2;
console.log('perimeter is :' + perimeter);
}
Acreage(){
let acreage = this.length * this.width;
console.log('acreage is :' + acreage);
}
}
let circle = new Circle('hinh tron',6);
circle.Perimeter(); // Perimeter is:37.68
circle.Acreage(); // Acreage is:113.03999999999999
let square = new Square('hinh cn',8,9);
square.Perimeter(); // Perimeter is :34
square.Acreage(); // Acreage is :72
Nhận thấy
cùng là một phương thức tính chu vi và diện tích được kế thừa từ class Shape
nhưng với các đối tượng khác nhau, thuộc tính khác nhau thì thực thi khác nhau
Lời kết (conclusion)
TypeScript vẫn đang trong quá trình phát triển. Trong quá
trình nguyên cứu TypeScript, tôi chỉ thấy cái khó của nó nằm ở cú pháp mới lạ.
Còn về phần tính năng, khái niệm thì khá quen với các ngôn ngữ hướng đối tượng
khác như C# hay PHP
Ngoài ra hiện nay rất nhiều thư viện cũng như framework của JavaScipt đã chuyển sang sử dụng cú pháp của TypeScript nên đây sẽ là ngôn ngữ rất đáng để đầu tư trong tương lai
Ngoài ra hiện nay rất nhiều thư viện cũng như framework của JavaScipt đã chuyển sang sử dụng cú pháp của TypeScript nên đây sẽ là ngôn ngữ rất đáng để đầu tư trong tương lai
Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer learn from TypeScript Training in Chennai . or learn thru Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry. Javascript Online Training from India
ReplyDeleteGreat Article. Thank you for sharing! Really an awesome post for every one.
DeleteProject Centers in Chennai
JavaScript Training in Chennai
Final Year Project Domains for IT
JavaScript Training in Chennai
ReplyDeleteSome us know all relating to the compelling medium you present powerful steps on this blog and therefore strongly encourage
contribution from other ones on this subject while our own child is truly discovering a great deal.
Have fun with the remaining portion of the year.
Selenium training in bangalore
Selenium training in Chennai
Selenium training in Bangalore
Selenium training in Pune
Selenium Online training
I want to tell you about a single site original casino slot machines There are many slots and slot machines. Withdrawing money is safe and fast. I play more than a month no complaints no.
ReplyDeleteGood Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
ReplyDeleteSelenium Training in Electronic City
The article is so informative. This is more helpful for our
ReplyDeleteBest online software testing training course institute in chennai with placement
Best selenium testing online course training in chennai
Learn best software testing online certification course class in chennai with placement
Thanks for sharing.
Great post and informative blog.it was awesome to read, thanks for sharing this great content to my vision.
ReplyDeleteGood discussion.
Java Training in Chennai
Java Training in Coimbatore
Java Training in Bangalore
The blog which you have shared is more creative... Waiting for your upcoming data...
ReplyDeletePython Training in Chennai
Python course in Chennai
Python Classes in Chennai
Best Python Training Institute in Chennai
Training in Tnagar
training in Thiruvanmiyur
Big data training in chennai
Software testing training in chennai
Selenium Training in Chennai
JAVA Training in Chennai
valuable blog thanks for sharing it...waiting for next update...
ReplyDeleteMobile Testing Training in Chennai
Mobile Application Testing Training in Chennai
Mobile Appium Training in chennai
Mobile Testing Training in Anna Nagar
Mobile Testing Training in T Nagar
Manual Testing Training in Chennai
LoadRunner Training in Chennai
Photoshop Classes in Chennai
Spring Training in Chennai
QTP Training in Chennai
Nice article,i really admire after reading this blog....
ReplyDeleteBest Aviation Academy in Chennai
Air Hostess Academy in Chennai
Airline Courses in Chennai
Ground Staff Training in Chennai
Airport Management Courses in Bangalore
Airport Management Courses in Chennai
Air Hostess Academy in Chennai
Air Hostess Course in Mumbai
Ground staff training in Bangalore
Best Aviation Academy in Chennai
Nice blog and valuable for all people. Thank you for posting this.
ReplyDeleteNode JS Training in Chennai
Node JS Course
Best IELTS Coaching in Chennai
learn Japanese in Chennai
Best Spoken English Class in Chennai
TOEFL Coaching Centres in Chennai
Node JS Training in OMR
Node JS Training in Tambaram
I feel satisfied to read your blog, you have been delivering a useful & unique information to our vision.keep blogging.
ReplyDeleteRegards,
Data Science Course in Chennai
Data Science Certification in Chennai
Data Science Training Institute in Chennai
R Training in Chennai
RPA Training in Chennai
DevOps certification in Chennai
Data Science course in Velachery
Data Science course in Tambaram
Data Science Training in Porur
Excellent blog!!! I got to know the more useful information by reading your blog. Thanks for posting this blog.
ReplyDeleteSpoken English Classes in Chennai
Best Spoken English Classes in Chennai
IELTS Coaching in Chennai
IELTS Coaching Centre in Chennai
English Speaking Classes in Mumbai
English Speaking Course in Mumbai
IELTS Classes in Mumbai
IELTS Coaching in Mumbai
IELTS Coaching in Anna Nagar
Spoken English Class in Anna Nagar
Great, this article is quite awesome and I have bookmarked this page for my future reference.
ReplyDeleteTally course in Chennai
Tally classes in Chennai
Tally Training in Chennai
ccna course in Chennai
PHP Training in Chennai
Salesforce Training in Chennai
Web Designing Training in Chennai
Tally Course in Ambattur
Tally Course in Perambur
Tally Course in Avadi
Great blog...! This blog contains useful information about this topic. Thank you for sharing this blog and can you do more updates & sharing like from this blog.
ReplyDeleteExcel Training in Chennai
Excel Course in Chennai
Tableau Training in Chennai
Linux Training in Chennai
Oracle Training in Chennai
Job Openings in Chennai
Oracle DBA Training in Chennai
Pega Training in Chennai
corporate training in chennai
Power BI Training in Chennai
Excel Training in Tambaram
Excellent post! It is really informative to all.keep update more information about this.
ReplyDeleteAngularJS Training in Velachery
AngularJS Training in Tambaram
AngularJS Training in OMR
AngularJS Training in T nagar
AngularJS Training in Thiruvanmiyur
AngularJS Training in Adyar
AngularJS Training in Porur
AngularJS Training in Vadapalani
AngularJS Training in Anna Nagar
This was an excellent post and very good information provided,Thanks for sharing.
ReplyDeletePython training in chennai
Python course in chennai
Python Training in OMR
DevOps training in anna nagar
PHP Course in chennai
Python Training in T.Nagar
Tally course in chennai
DOTNET training in chennai
Thanks for sharing informative article with us..
ReplyDeleteDOT NET Training in Chennai
asp .net training in chennai
DOT NET Training Institute in Chennai
.net course in chennai
dot net training in vadapalani
Html5 Training in Chennai
Spring Training in Chennai
Struts Training in Chennai
Wordpress Training in Chennai
SAS Training in Chennai
thanks for your information really good and very nice web design company in velachery
ReplyDeleteNice blog! Full of informative ideas. Thank you, keep sharing.
ReplyDeleteweb design company in chennai
tndte
ReplyDeleteI am happy for sharing on this blog its awesome blog I really impressed. thanks for sharing.
ReplyDeleteStart your journey with In Software Training in Bangalore and get hands-on Experience with 100% Placement assistance from experts Trainers @eTechno Soft Solutions Located in BTM Layout Bangalore.
ReplyDeleteThanks for your extraordinary blog. Your idea for this was so brilliant. This would provide people with an excellent tally resource from someone who has experienced such issues. You would be coming at the subject from a different angle and people would appreciate your honesty and frankness. Good luck for your next blog!
Tally ERP 9 Training
tally classes
Tally Training institute in Chennai
Tally course in Chennai
seo training classes
seo training course
seo training institute in chennai
seo training institutes
seo courses in chennai
seo institutes in chennai
seo classes in chennai
seo training center in chennai
python course in coimbatore
ReplyDeletejava course in coimbatore
python training in coimbatore
java training in coimbatore
php course in coimbatore
php training in coimbatore
android course in coimbatore
android training in coimbatore
datascience course in coimbatore
datascience training in coimbatore
ethical hacking course in coimbatore
ethical hacking training in coimbatore
artificial intelligence course in coimbatore
artificial intelligence training in coimbatore
digital marketing course in coimbatore
digital marketing training in coimbatore
embedded system course in coimbatore
embedded system training in coimbatore
Nice blog, it’s so knowledgeable, informative, and good looking site. I appreciate your hard work. Good job. Thank you for this wonderful sharing with us. Keep Sharing.
ReplyDeleteDigital Marketing Course In Kolkata
Web Design Course In Kolkata
Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
ReplyDeleteSelenium Training in Electronic City
This excellent website definitely has all of the information I needed about this subject and didn’t know subject who to ask.
ReplyDeleteGet real time project based and job oriented Salesforce training India course materials for Salesforce Certification with securing a practice org, database terminology, admin and user interface navigation and custom fields creation, reports & analytics, security, customization, automation and web to lead forms.
ReplyDeleteIf you are looking for a job oriented practical based workday course syllabus curriculum at Workday training institutes in India then you are arrived at the right place called Workday training institutes in ameerpet because this institute is quite popular for this wonderful course not just in Workday training institutes in Jaipur and also proving job assistance for Workday training institutes in Pune.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteGreat post! I am actually getting ready to across this information, It’s very helpful for this blog. Also great with all of the valuable information you have Keep up the good work you are doing well.
ReplyDeleteCRS Info Solutions Salesforce training for beginners
Nice! you are sharing such helpful and easy to understandable blog. i have no words for say i just say thanks because it is helpful for me.
ReplyDeleteDot Net Training in Chennai | Dot Net Training in anna nagar | Dot Net Training in omr | Dot Net Training in porur | Dot Net Training in tambaram | Dot Net Training in velachery
There are many slots and slot machines. Withdrawing money is safe and fast. Also great with all of the valuable information you have Keep up the good work you are doing well.
ReplyDeletepython training in chennai
python online training in chennai
python training in bangalore
python training in hyderabad
python online training
python flask training
python flask online training
python training in coimbatore
I just got to this amazing site not long ago. I was actually captured with the piece of resources you have got here. Big thumbs up for making such wonderful blog page!
ReplyDeleteoracle training in chennai
oracle training in tambaram
oracle dba training in chennai
oracle dba training in tambaram
ccna training in chennai
ccna training in tambaram
seo training in chennai
seo training in tambaram
Excellent post, From this post i got more detailed information,
ReplyDeleteThanks to share with us,
java training in chennai
java training in porur
aws training in chennai
aws training in porur
python training in chennai
python training in porur
selenium training in chennai
selenium training in porur
Great, this article is quite awesome and I have bookmarked this page for my future reference.
ReplyDeleteweb designing training in chennai
web designing training in omr
digital marketing training in chennai
digital marketing training in omr
rpa training in chennai
rpa training in omr
tally training in chennai
tally training in omr
There have been 20 hours of service outrage, which is even said to effect Salesforce. Salesforce training in Hyderabad
ReplyDeleteThanks for Sharing This Article.It is very so much valuable content. I hope these Commenting lists will help to my website
ReplyDeletetop workday online training
Really wonderful and interesting blog, I enjoyed reading this post.
ReplyDeletewhat is seo content writing
german language to english
salesforce basics
star certification
free hacking books
tableau real time interview questions
content about this title and keep updating here...
ReplyDeleteInformatica Training in Bangalore
Informatica Training in Chennai
Informatica MDM Training in Chennai
Informatica Course in Chennai
education and e learning services
ReplyDeleteecommerce seo services
b2b seo services
enterprise seo services
Good job...! I really happy to visit your post and thank you for your sharing...
ReplyDeleteReact JS Training in Chennai
Blue Prism Training in Bangalore
Blue Prism Training in Chennai
Blue Prism Online Training
Blue Prism Online Course
This information is really awesome thanks for sharing most valuable information.
ReplyDeleteworkday software training
workday training online