Skip to content

Instantly share code, notes, and snippets.

@blendsdk
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blendsdk/8180f5cccb76b36c2b1b to your computer and use it in GitHub Desktop.
Save blendsdk/8180f5cccb76b36c2b1b to your computer and use it in GitHub Desktop.
TypeScript singleon pattern
/**
* 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