Typescript
- 添加了类型注解等类型的功能
- `let num: number = 10`
- 添加了interface定义, abstract, private, public, protected
- 学习案例
// 写在立即执行函数中, 否则 let 可能会报错 (function() {
// 非常类似 Java 和 C# let num: number = 10 let isMarried: boolean = false let name: string = 'Jackson' interface IWalk { /** * walk */ walk(): void; } abstract class AnimalBase { private _name: string; private _age: number; constructor(name: string, age: number) { this._name = name this._age = age } abstract say(): void; get name(): string { return this._name } set name(name) { this._name = name } get age(): number { return this._age } set age(age) { this._age = age } }
class Dog extends AnimalBase implements IWalk { say(): void { console.log('Woof!') } walk() { console.log('Jump!') } } let dog: Dog = new Dog('Lily', 1) console.log(dog.name) console.log(name) dog.say() let walk: IWalk = new Dog('Jack', 2) walk.walk() let dogs: Array<Dog> = [] dogs.push(dog) dogs.push(new Dog('Tom', 2)) console.log(dogs) let myTuple: [number, boolean, string] = [num, isMarried, name] console.log(myTuple) })() |