SOURCE

var Singleton = function (name) {
    this.name = name
}

Singleton.instance = null

Singleton.prototype.getName = function () {
    return this.name
}

Singleton.getInstance = function ( name ) {
    if ( !this.instance ) {
        this.instance = new Singleton(name)
    } else {
        this.instance.name = name
    }
    return this.instance
}

var a = Singleton.getInstance('liucheng')
var b = Singleton.getInstance('liucheng2')

console.log(a.getName())
console.log(b.getName())

window.alert('判断两个是否是一个实例:' + (a === b))
console 命令行工具 X clear

                    
>
console