typescript 打字稿匿名类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42766986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:20:40  来源:igfitidea点击:

Typescript anonymous class

typescript

提问by user1137582

Is there a way in typescript to create anonymous class?

打字稿中有没有办法创建匿名类?

my code:

我的代码:

export abstract class Runnable {
    public abstract run();
}

and I'm trying to do something like this:

我正在尝试做这样的事情:

new Runnable {
    runner() {
        //implement
    }
}

How can I do it?

我该怎么做?

回答by Nitzan Tomer

Not quite, but you can do this:

不完全是,但你可以这样做:

abstract class Runnable {
    public abstract run();
}

let runnable = new (class MyRunnable extends Runnable {
    run() {
        console.log("running...");
    }
})();

runnable.run(); // running...

(code in playground)

操场上的代码

The problem with this approach however is that the interpreter will evaluate the class every time it uses it, unlike with a compiled language (such as java) in which the compiler only evaluates it once.

然而,这种方法的问题在于,解释器每次使用类时都会对其进行评估,这与编译器只对其进行一次评估的编译语言(例如 java)不同。

回答by Michal

Yes, here is the way.

是的,这就是方法。

The abstract class:

抽象类:

export abstract class Runnable {
  public abstract run();
}

An anonymous implementation:

匿名实现:

const runnable = new class extends Runnable {
  run() {
    // implement here
  }
}();

回答by holi-java

How to create Anonymous Class?

如何创建匿名类?

Let's say you have a interface Runnableand an abstract class Task.when you declare a class Fooin typescript you actual create an class instance of Foo& a constructor function for the class Foo.you could want to see depth in typescript.Anonymous class that ref as a constructor function like {new(...args):type}that can be created using newkeyword.

比方说,你有一个接口Runnable和抽象类Task。当你声明一个类Foo的打字稿你实际创建的类实例Foo及该类的构造函数Foo。你可能希望看到深度打字稿.Anonymous类,裁判的构造函数像{new(...args):type}这样可以使用new关键字创建。

interface Runnable {
    run(): void;
}

abstract class Task {
    constructor(readonly name: string) {
    }

    abstract run(): void;
}

Create anonymous class extends superclass via class extends ?

通过创建匿名类扩展超类 class extends ?

test('anonymous class extends superclass by `class extends ?`', () => {
    let stub = jest.fn();
    let AntTask: {new(name: string): Task} = class extends Task {
        //anonymous class auto inherit its superclass constructor if you don't declare a constructor here.
        run() {
            stub();
        }
    };

    let antTask: Task = new AntTask("ant");
    antTask.run();

    expect(stub).toHaveBeenCalled();
    expect(antTask instanceof Task).toBe(true);
    expect(antTask.name).toBe("ant");
});

create anonymous class implements interface/type via class ?.

创建匿名类通过class ?.

test('anonymous class implements interface by `class ?`', () => {
    let stub = jest.fn();
    let TestRunner: {new(): Runnable} = class {
        run = stub
    };

    let runner: Runnable = new TestRunner();
    runner.run();

    expect(stub).toHaveBeenCalled();
});