雖然JavaScript本身是一種物件導向語言,但並未提供對類別的支援,主要採用原型的方式實現物件建構、既成的功能,不過這對開發者並不友善。
var people = {
name:“小傑”,
sayHi(){
console.log("你好,我是"+this.name);
}
}
people.sayHi();
class People {
name:string;
constructor(name:string){
this.name = name
}
sayHi() {
console.log("你好,我是"+this.name);
}
}
var p = new People("小王");
p.sayHi();
class Teacher extends People {
subject:string
constructor(name:string, subject:string){
super(name)
this.subject = subject
}
sayHi() {
super.sayHi()
console.log("同學們好“);
}
teach() {
console.log(this.name + "進行教學" + this.subject);
}
}
var t = new Teacher("李老師",“TypeScript”);
t.teach();
t.sayHi();
今天的內容大致到這邊,明天會接著介紹關於類別的其他資訊!