Last active
August 29, 2015 14:22
-
-
Save blendsdk/8180f5cccb76b36c2b1b to your computer and use it in GitHub Desktop.
TypeScript singleon pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Simple singleton pattern in TypeScript | |
*/ | |
module Utils { | |
class GreeterSingleton { | |
public sayHello(name:string) : void { | |
alert(`Hello ${name}!`); | |
} | |
} | |
export var Greeter = new GreeterSingleton(); | |
} | |
Utils.Greeter.sayHello('World'); | |
////////////////////////////////////////////////////// | |
module Module_As_Singleton { | |
var prop1 = 'prop1' | |
export function test1() { | |
console.log(this.prop1 = 'prop1 original'); | |
} | |
export function test2() { | |
console.log(this.prop1 = 'prop1 changed'); | |
} | |
} | |
Module_As_Singleton.test2(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment