函数

Tutorial: TS初级二 Category: TS Published: 2026-04-07 13:58:26 Views: 20 Likes: 0 Comments: 0
  1. 函数的基本使用
export default {};

// 匿名函数
const makeMoney = function (salary: number, reward: number): number {
  return salary + reward;
};

// 命名函数
function writeCode(hour: number, sleep: number) {
  return hour;
}

// 箭头函数
const seeMeiMei = (time: number): void => {
  console.log(`我每天要看${time}个小时的MeiMei`);
};
seeMeiMei(8);

// 接口函数
type myFunc = (x: number, y: number) => number;
const myfunc: myFunc = (a, b) => a + b;
  1. 函数参数的处理
export default {};

// 可选参数
const func1: (x: number, y?: number) => number = function (a, b) {
  return a;
};

const func2 = function (a: number, b?: number): number {
  return a;
};

func2(10);
func2(10, 20);
func2(10, undefined);

// 函数的默认值
const func3 = function (
  a: number = 10,
  b: number = 20,
  c: number = 30
): number {
  return a + b + c;
};

func3();
func3(10);
func3(10, 20);
func3(10, 20, 30);

// 函数的剩余参数
const func4 = function (...args: any[]) {
  console.log(args);
};

func4(10, 20, 30, "邱淑贞");

const func5 = function (a: number, b: number, ...args: any[]) {
  console.log(a); // 10
  console.log(b); // 20
  console.log(args); // [ 30, '邱淑贞', '邢菲' ]
};

func5(10, 20, 30, "邱淑贞", "邢菲");
  1. 构造函数
export default {};

// 构造函数
var myFunction = new Function("a", "b", "return a*b");
var x = myFunction(4, 3);
console.log(x);

// 递归函数
function sum(arr: number[], n: number): number {
  if (n <= 0) {
    return 0;
  } else {
    return sum(arr, n - 1) + arr[n - 1];
  }
}
// sum([2, 3, 4, 5], 2) + 4
// sum([2, 3, 4, 5], 1) + 3 + 4
// sum([2, 3, 4, 5], 0) + 2 + 3 + 4
// 0 + 2 + 3 + 4 = 9

let res = sum([2, 3, 4, 5], 3);
console.log(res);
  1. 函数重载
export default {};

// 不使用函数重载
function add(a: number, b: number) {
  return a + b;
}

add(10, 20);

function add2(a: string, b: string) {
  return a + b;
}
add2("我的女神是: ", "邱淑贞");

function add3(a: string | number, b: string | number) {
  // return a + b;
  if (typeof a == "number" && typeof b == "number") {
    return a + b;
  }
  if (typeof a == "string" && typeof b == "string") {
    return a + b;
  }
  if (typeof a == "string" && typeof b == "number") {
    return a + b;
  }
  if (typeof a == "number" && typeof b == "string") {
    return a + b;
  }
}

add3(10, 20);
add3("邱淑贞", "景甜");
add3(10, "孟子义");
add3("杨紫", 20);

// 函数重载
function addFunc(a: number, b: number): number;
function addFunc(a: string, b: string): string;
function addFunc(a: string, b: number): string;
function addFunc(a: number, b: string): string;

function addFunc(a: any, b: any): any {
  return a + b;
}

addFunc(10, 20);
addFunc("邱淑贞", "景甜");
addFunc(10, "孟子义");
let res = addFunc("杨紫", 20);
console.log(res);

// 定义参数类型与参数数量不同
function star(s1: string): void;
function star(n1: number, s1: string): void;

function star(x: any, y?: any): void {
  console.log(x);
  console.log(y);
}

star("王心凌");
star("爱你");
star(1, "爱你");
  1. this 的使用
export default {};

let userInfo = {
  name: "邱淑贞",
  age: 18,
  song: "恨你太无情",
  marry: true,
  show: function () {
    this.marry = false;
  },
};

class Rectangle1 {
  w: number;
  h: number;

  constructor(w: number, h: number) {
    this.w = w;
    this.h = h;
  }

  getArea() {
    return () => {
      return this.w * this.h;
    };
  }
}

class Rectangle2 {
  w: number;
  h: number;

  constructor(w: number, h: number) {
    this.w = w;
    this.h = h;
  }

  getArea(this: Rectangle2) {
    return () => {
      return this.w * this.h;
    };
  }
}

class Rectangle3 {
  w: number;
  h: number;

  constructor(w: number, h: number) {
    this.w = w;
    this.h = h;
  }

  getArea(this: void) {
    return () => {
      return 111;
    };
  }
}
  1. 特殊的函数返回值
export default {};

type VoidFunc = () => void;

// 在类型别名中指定函数返回值为Void, 我们可以强行给它返回值 这个返回值是有效的
let func1: VoidFunc = function () {
  console.log("哈哈哈");
  return "邱淑贞";
};

// 在函数指定返回值为void
function func2(): void {
  // return "邱淑贞"
}
Prev: 接口-2 Next: 类-1