01 – function

/ /! Function overloading
/ /! The names of functions are the same, but the parameters and numbers of functions are different
(() = > {
  // Define a function
  /** ** We have an add function that accepts two string arguments for concatenation and two number arguments for addition */
  // Function overload declaration
  function name(x: string, y: string) :string;
  function name(x: number, y: number) :number;
  // Function declaration
  function name(x: number | string, y: number | string) :number | string {
    if (typeof x === "string" && typeof y === "string") {
      return x + y;
    } else if (typeof x === "number" && typeof y === "number") {
      return x + y;
    }
    return "";
  }
  // Function call
  console.log(name("Various ge".The bore is clear ""));
  console.log(name(10.10));
  / /? If the incoming data is illegal
  //console.log(name(" ao ", 10));}) ();Copy the code

02- Optional and default parameters

/ /! When an optional argument function is declared, the internal argument is used. This parameter may or may not be passed and may be called optional
/ /! When a function is declared, its internal parameters have their own default values, which are called default parameters
(() = > {
  / /? Define a function
  /** ** If you pass in the first and last name, you get the first name (last name + first name = first name) ** Required: If nothing is passed in, return the default last name ** Required: If only the last name is passed in, return the last name ** Required: If the last name and first name are passed in, return the first name */
  const getName = function (firstName: string = "Make", lastName? : string) {
    / /? Checks whether the name is passed in
    if (lastName) {
      return firstName + "_" + lastName;
    } else {
      returnfirstName; }};/ /? A function call
  // Nothing
  console.log(getName());
  // Only the surname is passed in
  console.log(getName("Various ge"));
  // Pass in the last name and first name
  console.log(getName("Various ge"."Cao cao")); }) ();Copy the code

03- Remaining parameters

/ /! Remaining parameter REST
/ * * *! The remaining arguments are placed at the end of all arguments in the function declaration
(() = > {
  / /? . Args: string[] ----> The remaining arguments are placed in an array of strings
  function name(str: string, ... args: string[]) {
    console.log("str: ", str); // Get an A
    console.log("args: ", args); // Get a b c
  }
  name("a"."b"."c"); }) ();Copy the code

04- Function overloading

/ /! Function overloading
/ /! The names of functions are the same, but the parameters and numbers of functions are different
(() = > {
  // Define a function
  /** ** We have an add function that accepts two string arguments for concatenation and two number arguments for addition */
  // Function overload declaration
  function name(x: string, y: string) :string;
  function name(x: number, y: number) :number;
  // Function declaration
  function name(x: number | string, y: number | string) :number | string {
    if (typeof x === "string" && typeof y === "string") {
      return x + y;
    } else if (typeof x === "number" && typeof y === "number") {
      return x + y;
    }
    return "";
  }
  // Function call
  console.log(name("Various ge".The bore is clear ""));
  console.log(name(10.10));
  / /? If the incoming data is illegal
  //console.log(name(" ao ", 10));}) ();Copy the code