Last active
November 28, 2015 16:44
-
-
Save AlanJui/c4b2f89939be08b1d564 to your computer and use it in GitHub Desktop.
實作 TypeScript 在 Class, Interface 的應用。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface IWellcome { | |
greet(): any; | |
} | |
class User { | |
firstName: string; | |
lastName: string; | |
constructor(firstName: string, lastName: string) { | |
this.firstName = firstName; | |
this.lastName = lastName; | |
this.logUser(); | |
} | |
private fullName(): string { | |
return `${this.firstName} ${this.lastName}`; | |
} | |
getFullName(): string { | |
return this.fullName(); | |
} | |
logUser(): any { | |
let time = new Date(); | |
console.log(`Time: ${time.toLocaleDateString()} ${time.toTimeString()} ==> User ${this.getFullName()} added`); | |
} | |
} | |
class Member extends User implements IWellcome { | |
constructor(firstName: string, lastName: string) { | |
super(firstName, lastName); | |
} | |
greet(): void { | |
alert(`Hello ${this.getFullName()}`); | |
} | |
} | |
let member = new Member('Alan', 'Jui'); | |
var button = document.createElement('button'); | |
button.textContent = "Say Hello"; | |
button.onclick = function () { | |
alert(member.greet()); | |
}; | |
document.body.appendChild(button); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
以上之 TypeScript 原始程式程式碼,可在 TypeScript 官網(URL: http://www.typescriptlang.org/ )「試玩」。
操作方法:
http://goo.gl/NvakSp