/typescript/

TypeScript part 23 (singleton)

2017-03-04 21:01:03

TypeScript

class Config {

    private static instance: Config;
    private environment = "dev";

    private constructor() {
    }

    static getInstance(): Config {
        if (!Config.instance) {
            Config.instance = new Config();
        }
        return Config.instance;
    }

    public getEnvironment(): string {
        return this.environment;
    }
}

const god = Config.getInstance();
console.log(god.getEnvironment());

JavaScript

var Config = (function () {
    function Config() {
        this.environment = "dev";
    }
    Config.getInstance = function () {
        if (!Config.instance) {
            Config.instance = new Config();
        }
        return Config.instance;
    };
    Config.prototype.getEnvironment = function () {
        return this.environment;
    };
    return Config;
}());
var god = Config.getInstance();
console.log(god.getEnvironment());