SOURCE

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

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

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

var a = Singleton.getInstance('liu 1')
var b = Singleton.getInstance('liu 2')

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

alert('a 和 b 是否是一个单例:' + (a === b))
console 命令行工具 X clear

                    
>
console