javascript - TypeScript and JS-like singleton pattern - Stack Overflow

I need to implement Singleton pattern in TypeScript. I have found a solution here but it seems like an

I need to implement Singleton pattern in TypeScript. I have found a solution here but it seems like an overkill for me. After all, JavaScript is great in creating singletons.

Is it possible to write in TypeScript something like this (without getting an error)?

module Helpers {
    Helpers.Singleton = {};
}

Currently, it correctly generates the output that I am expecting:

// Module
var Helpers;
(function (Helpers) {
    Helpers.Singleton = {};
})(Helpers || (Helpers = {}));

But why is TypeScript piler inside my VisualStudio plaining about that (it says that it "Could not find symbol 'Helpers'." at line 2!)? Is it possible to write it in another way?

I need to implement Singleton pattern in TypeScript. I have found a solution here but it seems like an overkill for me. After all, JavaScript is great in creating singletons.

Is it possible to write in TypeScript something like this (without getting an error)?

module Helpers {
    Helpers.Singleton = {};
}

Currently, it correctly generates the output that I am expecting:

// Module
var Helpers;
(function (Helpers) {
    Helpers.Singleton = {};
})(Helpers || (Helpers = {}));

But why is TypeScript piler inside my VisualStudio plaining about that (it says that it "Could not find symbol 'Helpers'." at line 2!)? Is it possible to write it in another way?

Share Improve this question edited Nov 4, 2013 at 15:55 Roman Mazur asked Nov 4, 2013 at 15:47 Roman MazurRoman Mazur 5126 silver badges17 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10
module Helpers {
    export module Singleton {
        export var etc = 4;
        export function printSomething() {
            // etc
        }
    }
}

// later...
Helpers.Singleton.printSomething();

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744215840a4563549.html

相关推荐

  • javascript - TypeScript and JS-like singleton pattern - Stack Overflow

    I need to implement Singleton pattern in TypeScript. I have found a solution here but it seems like an

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信