相关疑难解决方法(0)

打字稿中的“静态方面”和“实例方面”究竟是什么?

有一个非常相似的问题,但是文档中提到的静态端实例端没有直接答案。

正如我依稀理解的那样,静态端是构造函数,实例端是其他一切?

打字稿文档中的一个示例:

interface ClockConstructor {
    new (hour: number, minute: number): ClockInterface;
}
interface ClockInterface {
    tick(): void;
}

function createClock(ctor: ClockConstructor, hour: number, minute: number): ClockInterface {
    return new ctor(hour, minute);
}

class DigitalClock implements ClockInterface {
    constructor(h: number, m: number) { }
    tick() {
        console.log("beep beep");
    }
}
class AnalogClock implements ClockInterface {
    constructor(h: number, m: number) { }
    tick() {
        console.log("tick tock");
    }
}

let digital …
Run Code Online (Sandbox Code Playgroud)

typescript

3
推荐指数
1
解决办法
661
查看次数

为类编写Typescript接口

我正在阅读Typescript手册的类类型部分,我对如何编写类的接口定义感到困惑.从文档中,我了解一个接口可以用来描述Class的"实例"方面.但是,您如何编写描述类的"静态"一面的界面?

这是一个例子:

interface IPerson {
    name: string;
    getName(): string;
}

class Person implements IPerson {
    public name: string;

    constructor(name: string) {
        this.name = name;
    }

    public getName() {
        return this.name;
    }
}
Run Code Online (Sandbox Code Playgroud)

在这个例子中,你将如何修改,IPerson以便也可以描述构造函数?

javascript typescript typescript1.8

2
推荐指数
1
解决办法
447
查看次数

标签 统计

typescript ×2

javascript ×1

typescript1.8 ×1