TypeScript函数

函数的基本使用

介绍

函数是JavaScript应用程序的基础。它帮助你实现抽象层,模拟类,信息隐藏和模块。在TypeScript里,虽然已经支持类,命名空间和模块,但函数仍然是主要的定义行为的地方。TypeScript为javaScript函数添加了额外的功能,让我们可以更容易地使用

函数定义的方式

export default{};

// 匿名函数
const MakeMoney = function(salary:number,reward:number):number{
    return salary + reward;
}
let res = MakeMoney(1000,500);
console.log(res);


// 有名函数|命名函数|普通函数
function writeCode(hour:string,sleep:number){
    return hour;
}
console.log('小明',20+'岁');


// 箭头函数
const seeMeimei = (time:number):void =>{
    console.log(`·我每天都要看${time}个小时meimei`);
}

seeMeimei(9);

构造函数

TypeScript也支持使用JavaScript内置的构造函数Function()来定义函数;

export default{};

// 构造函数
const myFun = new Function('a','b','return a * b');
var res = myFun;
console.log(res(2,5));

函数重载

重载是方法名字相同,而参数不同,返回类型可以相同也可以不同。

每个重载的方法(或者构造函数)都必须有一个独一无二的参数类型列表。

export default{};

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

//使用函数重载
function addFunc(a:any, b:any):any{
    return a + b;
} 
var res = addFunc(10, 10);
console.log(res);

console.log(addFunc('小明','小离'));
console.log(addFunc(20,'小离'));
console.log(addFunc('小明',21));

类的使用

类的基本使用

介绍

传统的JavaScript程序使用函数和基于原型的继承来创建可重用的组件,但对于熟悉使用面向对象方式的程序员来讲就有些棘手,因为他们用的是基于类的继承并且对象是由类构建出来的。 从ECMAScript 2015,也就是ECMAScript 6开始,JavaScript程序员将能够使用基于类的面向对象的方式。 使用TypeScript,我们允许开发者现在就使用这些特性,并且编译后的JavaScript可以在所有主流浏览器和平台上运行,而不需要等到下个JavaScript版本。

export default {}
// 创建一个类
class Person {
    name: string
    age: number

    constructor(name: string, age: number) {
        this.name = name;
        this.age = age
    }
    // 函数(方法)
    sayHello(): void {
        console.log(`我的女神是${this.name},她今年${this.age}岁了,但是在我心里她永远18岁!`);

    }
}
// 实例化
let p = new Person("成果", 27);
p.sayHello()

类的继承

在TypeScript里,我们可以使用常用的面向对象模式。 基于类的程序设计中一种最基本的模式是允许使用继承来扩展现有的类。

export default {}
// 创建一个类
class Person {
    name: string
    age: number

    constructor(name: string, age: number) {
        this.name = name;
        this.age = age
    }
    // 函数(方法)
    sayHello(): void {
        console.log(`我的女神是${this.name},她今年${this.age}岁了,但是在我心里她永远18岁!`);
    }
}
// 实例化
let p = new Person("成果", 27);
p.sayHello()
class Student extends Person {
    score: string
    constructor(name: string, age: number, score: string) {
        // 调用父类中的构造函数
        super(name, age)
        this.score = score
    }
    sayHello(): void {
        // 调用父类中的方法
        // super.sayHello()
        // 重写父类中的函数
        console.log(`我是重写的方法,我叫${this.name},今年${this.age}岁,我的评分是${this.score}`);
    }
}

let s = new Student("张三", 20, "sss")
s.sayHello()

static和instanceof

export default {}
class StaticTest {
    static salary: number
    static say(): void {
        console.log(`我们想要的工资是${this.salary}`);

    }
}


class Person { }
let p = new Person()
let isPerson = p instanceof Person;
console.log("p是Person实例化出来的吗?" + isPerson);

getter与setter

export default {};

// getter与setter
class MyName{
    private _fullName:string="杨幂";
    //读取字段的值
    get fullName():string{
        console.log("get被调用了");
        return this._fullName;
    }    

    // 为字段赋值
    set fullName(newName:string){
        console.log("set被调用了");
        this._fullName = newName;
    }
}
let starname = new MyName();
starname.fullName = "袁冰妍";

console.log(starname);
console.log(starname.fullName);


let n = new MyName();
n.fullName = "飞飞侠";

抽象类

抽象类做为其它派生类的基类使用。 它们一般不会直接被实例化。 不同于接口,抽象类可以包含成员的实现细节。 abstract关键字是用于定义抽象类和在抽象类内部定义抽象方法。

export default{};

abstract class Person{
    abstract name:string;
    abstract age:number;
    abstract show():string;    

    showName():void{
        console.log('Hello 老Baby');
    }
}

class Student extends Person{
    name: string = '刘亦菲'
    age:number = 20;
    show(){
        return 'Hello 老Baby';
    }
}
console.log(Person);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值