Mind mapping

Uses: functions, classes, constructors

Function: interface
interface serarchFunction {
  (keyword: string, page? :number) :string[];
}

// Return values must be consistent; Parameter can not write, write must correspond to
const search: serarchFunction = function (keyword: string, page: number) {
  return ['a'.'b'.'c'];
}
Copy the code
Class: interface
interface serialization {
  toJSON(): object;
  fromJSON(json: object) :void;

}

function isSize(json: any) :json is { width: number.height: number } {
  if (typeofjson ! = ='object') {
    throw new Error('must be a object');
  } else {
    if (typeofjson.width ! = ='number' || typeofjson.height ! = ='number') {
      throw new Error('width and height must be number'); }}return true;
}

class Box implements serialization {
  width: number;
  height: number;

  constructor(width: number, height: number) {
    this.width = width;
    this.height = height;
  }

  toJSON() {
    return { width: this.width, height: this.height };
  }

  fromJSON(json: object) :void {
    if (isSize(json)) {
      this.width = json.width;
      this.height = json.height; }}}function serialize(obj: serialization) {
  let json = obj.toJSON();
  let str = obj.fromJSON(json);

  console.log(str);
}

let box = new Box(66.88);

serialize(box);
Copy the code
abstract class SerarchFunction {
  abstract toJSON(): object;
  abstract fromJSON(json: object) :void;
}

function isSize(json: any) :json is { width: number.height: number } {
  if (typeofjson ! = ='object') {
    throw new Error('must be a object');
  } else {
    if (typeofjson.width ! = ='number' || typeofjson.height ! = ='number') {
      throw new Error('width and height must be number'); }}return true;
}

class Box extends SerarchFunction {
  width: number;
  height: number;

  constructor(width: number, height: number) {
    super(a);this.width = width;
    this.height = height;
  }

  toJSON() {
    return { width: this.width, height: this.height };
  }

  fromJSON(json: object) :void {
    if (isSize(json)) {
      this.width = json.width;
      this.height = json.height; }}}function serialize(obj: SerarchFunction) {
  let json = obj.toJSON();
  let str = obj.fromJSON(json);

  console.log(str);
}

let box = new Box(66.88);

serialize(box);
Copy the code
Inheritance VS interface
// class base1 {}

// class base2 {}

// class Child extends base1, base2 { }


interface inter1 { }

interface inter2 { }

class Child implements inter1.inter2 {}Copy the code

Inheritance: code reuse, complete hierarchical interface: flexibility

Constructor: interface
// // Error Example
// interface Boxinterface {
// new(a: string);

// show(a: number): void;
// }

// class Box implements Boxinterface {
// constructor(a: string) { }

// show(a: number) {

/ /}
// }

// Correct example
interface BoxConstructorInterface {
  new(a: string);
}

interface BoxInterface {
  show(a: number) :void;
}

const Box: BoxConstructorInterface = class implements BoxInterface {
  private a: string;

  constructor(a: string) {
    this.a = a;
  }

  show(a: number) {
    alert(this.a + a); }}Copy the code

Inheritance of interfaces

interface Box {
  area(): number;
  length(): number;
}

interface Box2 extends Box {
  volumn(): number;
}

class A implements Box2 {
  area() {
    return 11;
  }

  length() {
    return 22;
  }

  volumn() {
    return 33; }}Copy the code
class Box {
  private width: number;
}

interface BoxInter extends Box {
  user: string;
}

class A extends Box implements BoxInter {
  user: string;
}
Copy the code