- 函数的基本使用
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;
- 函数参数的处理
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);
console.log(b);
console.log(args);
};
func5(10, 20, 30, "邱淑贞", "邢菲");
- 构造函数
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];
}
}
let res = sum([2, 3, 4, 5], 3);
console.log(res);
- 函数重载
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) {
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, "爱你");
- 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;
};
}
}
- 特殊的函数返回值
export default {};
type VoidFunc = () => void;
let func1: VoidFunc = function () {
console.log("哈哈哈");
return "邱淑贞";
};
function func2(): void {
}