麻烦实例化一个对象

时间:2014-04-14 20:20:46

标签: javascript typescript

这一切都应该有效,但不是。我有以下打字稿代码:

module LayoutEngine {
    class WorkerApi {
        private worker : Worker;

        constructor() {
            debugger;
            this.worker = new Worker("layout-worker.js");
        }

我正在尝试在javascript中创建此对象:

require(["../common/events", "worker-api.js"], function (events) {
    var api = new LayoutEngine.WorkerApi();

但我得到Uncaught TypeError: undefined is not a function并在调试器中LayoutEngine does evaluate as an object but LayoutEngine.WorkerApi resolves as undefined

我在宣言中遗漏了什么?

1 个答案:

答案 0 :(得分:0)

您是否正确导出类?

我认为你需要"出口" WorkerApi类

所以你的代码应该是

module LayoutEngine {
    export class WorkerApi {
        private worker : Worker;

        constructor() {
            debugger;
            this.worker = new Worker("layout-worker.js");
        }

然后,像这样导入它们

var workerApi = require(["../common/events", "worker-api.js"];
var api = new workerApi();

希望这有帮助!