1、特点

  1. var
  2. - 作用域
  3. - 重复声明
  4. let
  5. - 块级作用域
  6. - 在同一个块中不能重复声明
  7. const
  8. - 声明同时必须赋值
  9. - 一旦声明不可改变(对象可以修改)
  10. - 块级作用域
  11. //强类型语言 声明变量要指明其类型
  12. var str:string = "hello world"

2、数据类型

  1. //number,string,boolean
  2. var num:number= 10;
  3. var str:string = "hello world";
  4. var b:boolean = true
  1. //Array
  2. var arr:string [] = ['html','css','javascript']
  3. var all:number [] = [ 1,2,3]
  4. // object
  5. var obj:object = {
  6. name:"lisi",
  7. age:14
  8. }
  1. //声明一个数组,里面的元素是object
  2. var list:object [] = [{name:"cheng",age:20},{name:"zhang",age:19}]

2-1 枚举类型

  1. // 枚举
  2. enum Status {
  3. success=200,
  4. fail=404,
  5. serverError=500
  6. }
  7. var s:Status = Status.success;
  8. console.log(s);

2-2 any

  1. // 指明数据类型为any后就不会类型检查了,数据可以改变成任意类型
  2. var data:any = [1,2,3];
  3. data = 'hello world';
  4. data = 1442;
  5. console.log(data);

2-3 void 没有返回值

  1. function go():void{
  2. console.log("hello world")
  3. }
  4. go()
  5. function test():number{
  6. console.log("a");
  7. return 10;
  8. }

2-4 null和undefined

  1. var n:null;
  2. n = 20; //报错,因为变量已经声明为null,不能改变为其他类型
  3. var s:null|number;
  4. s = 30;

2-5泛型(任意,类型检查)

  1. // 通俗理解:泛型就是解决类,接口,方法的复用性。以及对不特定数据类型的支持。
  2. // function getData(value:any){
  3. // console.log(value)
  4. // }
  5. // 局限性 只能传入特定的类型
  6. // any放弃了类型检查
  7. // 既然有类型检查,又想传想传什么就传什么
  8. // 任意类型: 任意,类型检查
  9. function getData<T>(value:T){
  10. console.log(value)
  11. }
  12. getData<string>("hello")
  13. getData<number>(113);
  1. class Person<T>{
  2. print(msg:T){
  3. console.log(msg)
  4. }
  5. }
  6. class Student{
  7. name:string;
  8. age:number;
  9. constructor(name:string,age:number){
  10. this.name = name;
  11. this.age = age;
  12. }
  13. }
  14. var p = new Student("cheng",20);
  15. var son = new Person<string>();
  16. son.print("hello")
  17. var li = new Person<Student>();
  18. li.print(p);

3、函数

3-1带参数的函数

  1. function fn(name:string,age:number):number{
  2. console.log(name,age)
  3. return 10;
  4. }
  5. fn("cheng",10)

3-2 函数的默认参数

  1. function fn(name:string,age:number=20):number{
  2. console.log(name,age)
  3. return 10;
  4. }
  5. fn("cheng")

3-3 函数的可选参数

  1. //设置可选参数之后,调用函数的时候,这个参数是可传可不传的
  2. function fn(name:string,age?:number):number{
  3. console.log(name,age)
  4. return 10;
  5. }
  6. fn("cheng")

4、class

  1. // private 私有的 作用范围只能类中
  2. // public 共有的 其他类也可以访问
  3. //不写变量修饰符 默认就是共有的
  4. //person.ts
  5. class Person {
  6. //类的一个属性
  7. name:string;
  8. age:number;
  9. constructor(name:string,age:number){
  10. this.name = name;
  11. this.age = age;
  12. }
  13. getName():void{
  14. console.log(this.name)
  15. }
  16. }
  17. export default Person;
  18. import Person from './person'
  19. // 继承 子类继承父类,子类的构造函数中第一行一定要加super
  20. // 子类中调用父类的方法,this,super都可以调用
  21. class Student extends Person {
  22. skill:string;
  23. constructor(name:string,age:number,skill:string){
  24. super(name,age);
  25. this.skill = skill;
  26. }
  27. }
  28. var s:Student = new Student("zhang",20,"lol");
  29. console.log(s);
  30. s.getName()

装饰器

  1. // 装饰器 在不修改类的前提下对类的拓展
  2. function addName(target:any){
  3. target.prototype.name = "cheng"
  4. }
  5. @addName
  6. class Person{
  7. getData(){
  8. console.log("hello world")
  9. }
  10. }
  11. var p:any= new Person();
  12. console.log(p.name);

5、接口

  1. // 接口 在面向对象的编程中,接口一中规范。定义了行为的规范,在程序设计中,接口起来了限制和规范的作用
  2. interface Animal {
  3. eat():any;
  4. run():any;
  5. }
  6. // 实现一个接口必须对里面的方法重写
  7. class Dog implements Animal {
  8. eat():void{
  9. console.log("吃骨头")
  10. }
  11. run():void{
  12. console.log("狗刨")
  13. }
  14. }