SOURCE

class Singleton {
    constructor(name) {
        this.name = name;
        this.instance = null
    }

    getName() {
        return this.name
    }

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

}


const single = new Singleton('jake')


const instance1 = single.getInstance('mike')
const instance2 = single.getInstance('mike')

console.log(instance1)
console.log(instance2)
console.log(instance1 === instance2)
console 命令行工具 X clear

                    
>
console