1.属性类接口

  1. // 对批量方法传入参数进行约束
  2. // 接口:行为和动作的规范,对批量方法进行约束
  3. interface FullName {
  4. firstName: string; // 注意以分号结尾
  5. lastName: string;
  6. }
  7. function printName(name: FullName) {
  8. // 必须传入对象firstName lastName
  9. console.log(name.firstName + " " + name.lastName);
  10. }
  11. printName("小三"); //错误
  12. printName({ firstName: "张", lastName: "三" });//正确,必须包含但不可以有其他对象属性
  13. printName({ firstName: "张", lastName: "三", age: 30 });//报错
  14. var obj = {//参数的顺序可以不一样
  15. //推荐写法
  16. firstName: "张",
  17. lastName: "三",
  18. age: 30,
  19. };
  20. printName(obj);
  21. //有问题
  22. interface FullName {
  23. firstName: string; // 注意以分号结尾
  24. lastName: string;
  25. }
  26. function printName(name: FullName) {
  27. // 必须传入对象firstName lastName
  28. console.log(name.firstName + " " + name.lastName+name.age);//接口中必须有age才行
  29. }
  30. var obj = {
  31. //推荐写法
  32. firstName: "张",
  33. lastName: "三",
  34. age: 30,
  35. };
  36. printName(obj);

image.pngimage.png
image.png

  1. 接口:可选属性
  2. interface FullName {
  3. firstName?: string;
  4. lastName?: string; //可选参数
  5. }
  6. function printName(name: FullName) {
  7. console.log(name.firstName + " " + name.lastName);
  8. }
  9. var obj = {
  10. // firstName: "张",
  11. // lastName: "三",
  12. };
  13. printName(obj);

image.pngimage.png

2.AJAX的请求过程解析

  1. // 举个例子:ajax的异步请求
  2. /* $.ajax({
  3. type: "GET",
  4. url: "test.json",
  5. data: "我是一段数据...",
  6. dataType: "json",
  7. }); */
  8. interface Config {
  9. type: string;
  10. url: string;
  11. data?: string;
  12. dataType: string;
  13. }
  14. function ajax(config: Config) {
  15. var xhr = new XMLHttpRequest();
  16. xhr.open(config.type, config.url, true);
  17. xhr.send(config.data);
  18. xhr.onreadystatechange = function () {
  19. if (xhr.readyState == 4 && xhr.status == 200) {
  20. console.log("请求发送成功");
  21. if (config.dataType == "json") {
  22. console.log(JSON.parse(xhr.responseText));
  23. } else {
  24. console.log(xhr.responseText);
  25. }
  26. }
  27. };
  28. }
  29. ajax({
  30. type: "GET",
  31. url: "http://www.migu.cn/",
  32. data: "name:张三",
  33. dataType: "json",
  34. });

2.可索引接口:(不常用)

  1. // ts中定义数组的方式
  2. let arr2: number[] = [1, 2, 3, 4];
  3. let arr3: Array<string> = ["aaa", "ddd", "fff"];
  4. // 可索引接口 对数组的约束:
  5. interface UserArr {
  6. [index: number]: string;
  7. }
  8. var arr: UserArr = [123, "bbb"]; //错误
  9. var arr1: UserArr = ["aaa", "bbb"]; //正确
  10. console.log(arr[1]);
  11. // 可索引接口 对对象的约束
  12. interface UserObj {
  13. [index: string]: string;
  14. }
  15. var obj: UserObj = { name: "张三" };
  16. console.log(obj.name);

3.类类型接口:

  1. // 类类型接口:对类的约束 和抽象类有点类似
  2. interface Animal {
  3. name: string;
  4. eat(str: string): void;
  5. }
  6. class Dog implements Animal {
  7. name: string;
  8. constructor(name: string) {
  9. this.name = name;
  10. }
  11. eat() {
  12. console.log(this.name + ",你蹲在厕所干嘛?");
  13. }
  14. }
  15. let d = new Dog("小黑");
  16. d.eat();
  17. class Cat implements Animal {
  18. name: string;
  19. constructor(name: string) {
  20. this.name = name;
  21. }
  22. eat(food: string) {
  23. console.log(this.name + "吃" + food);
  24. }
  25. }
  26. let c = new Cat("小花猫");
  27. c.eat("狗粮");

4.接口扩展:接口可以继承接口

  1. // 扩展接口
  2. interface Animal {
  3. eat(): void;
  4. }
  5. interface Person extends Animal {
  6. work(): void;
  7. }
  8. class Web implements Person {
  9. public name: string;
  10. constructor(name: string) {
  11. this.name = name;
  12. }
  13. eat() {
  14. console.log(this.name + ",你这么喜欢吃这个?我看错你了");
  15. }
  16. work() {
  17. console.log(this.name + ",你怎么又在写代码?");
  18. }
  19. }
  20. let w = new Web("小明");
  21. w.work();
  22. w.eat();
  23. //不仅可以继承父类还可以实现接口
  24. interface Animal {
  25. eat(): void;
  26. }
  27. interface Person extends Animal {
  28. work(): void;
  29. }
  30. class Programmer {
  31. public name: string;
  32. constructor(name: string) {
  33. this.name = name;
  34. }
  35. coding(code: string) {
  36. console.log(this.name + code);
  37. }
  38. }
  39. class Web extends Programmer implements Person {
  40. constructor(name: string) {
  41. super(name);
  42. }
  43. eat() {
  44. console.log(this.name + ",你这么喜欢吃这个?我看错你了");
  45. }
  46. work() {
  47. console.log(this.name + ",你怎么又在写代码?");
  48. }
  49. }
  50. let w = new Web("老王");
  51. w.work();
  52. w.eat();
  53. w.coding("写java");