TS 测试代码
const flag = true;
const num = 123;
console.log(flag, num);
function getInfo(name: string, age: number): string {
return `${
name}---${
age}`;
}
getInfo('xiaobai', 5);
function getInfo1(name: string, age?: number): string {
return `${
name}---${
age}`;
}
getInfo1('xiaobai', 5);
getInfo1('xiaobai');
function getInfo2(name: string, age = 33): string {
return `${
name}---${
age}`;
}
getInfo2('xiaobai', 5);
function getInfo3(name: string, age: number): void {
console.log(`${
name}---${
age}`);
}
getInfo3('xiaobai', 5);
function sum(...result: number[]): number {
const sum = 0;
for (let i = 0; i < result.length; i++) {
}
return sum;
}
console.log(sum(1, 2, 3, 4, 5, 6));
function sum1(init: number, ...result: number[]): number {
let sum = 0;
for (let i = 0; i < result.length; i++) {
sum += init;
}
return sum;
}
console.log(sum(100, 1, 2, 3, 4, 5, 6));
console.log(sum1(100, 1, 2, 3, 4, 5, 6));
function czFn(name: string): string;
function czFn(name: string, age: number): string;
function czFn(name: string, age?: string | number): string {
if (age) {
return '小白' + name + ',年龄:' + age;
} else {
return '小白' + name;
}
}
czFn('zhangsan');
czFn('lisi', 20);
class Person {
public name: string;
constructor(n: string) {
this.name = n;
}
public run(): void {
console.log(this.name + '在跑步');
}
}
const p = new Person('xiaobai');
p.run();
class Person1 {
name: string;
constructor(n: string) {
this.name = n;
}
run(): void {
console.log(this.name + '在跑步');
}
}
class Chinese extends Person1 {
age: number;
constructor(n: string, a: number) {
super(n);
this.age = a;
}
speak(): void {
super.run();
console.log(this.name + '说中文');
}
}
const c = new Chinese('xiaobai', 5);
c.speak();
class Person2 {
public name: string;
protected age: number;
constructor(n: string, a: number, _s: string) {
this.name = n;
this.age = a;
}
run(): void {
console.log(this.name + '在跑步');
}
}
const p2: Person2 = new Person2('xiaobai', 5, '男');
console.log(p2.name);
class Person3 {
public name: string;
static age = 12;
constructor(n: string) {
this.name = n;
}
run(): void {
console.log(this.name + '在跑步');
}
static getAge(): void {
console.log('年龄是', Person3.age);
}
}
Person3.age;
Person3.getAge;
const p3: Person3 = new Person3('xiaobai');
p3.run();
abstract class Animal {
name: string;
constructor(name: string) {
this.name = name;
}
run() {
console.log(this.name + '会跑');
}
}
class Dog extends Animal {
constructor(name: string) {
super(name);
}
}
const d: Dog = new Dog('巴哥');
d.run();
class Cat extends Animal {
constructor(name: string) {
super(name);
}
}
const cat: Cat = new Cat('花猫');
cat.run();
abstract class Animal1 {
name: string;
constructor(name: string) {
this.name = name;
}
abstract eat(): unknown;
run() {
console.log(this.name + '会跑');
}
}
class Dog1 extends Animal1 {
constructor(name: string) {
super(name);
}
eat(): void {
console.log(this.name + '吃骨头');
}
}
const d1: Animal1 = new Dog1('狗');
d1.eat();
class Cat1 extends Animal1 {
constructor(name: string) {
super(name);
}
eat(): void {
console.log(this.name + '吃老鼠');
}
}
const c1: Animal1 = new Cat1('猫');
c1.eat();
interface Animal2 {
name: string;
eat(str: string): void;
}
class Dog2 implements Animal2 {
name: string;
constructor(name: string) {
this.name = name;
}
eat() {
console.log(this.name + '吃骨头');
}
}
const d2 = new Dog2('狗');
d2.eat();
class Cat2 implements Animal2 {
name: string;
constructor(name: string) {
this.name = name;
}
eat(food: string) {
console.log(this.name + '吃' + food);
}
}
const c2 = new Cat2('猫');
c2.eat('鼠');
const array = [1, 2, 3, [4, 5]].flat();
switch (array[0]) {
case 1:
console.log('1');
break;
case 2:
console.log('2');
break;
default:
console.log('3');
break;
}